Ruby equivalent to simple awk program

$ ruby -e 'puts $ARGF.readlines.reverse'

Gennady.

···

-----Original Message-----
From: Bil Kleb [mailto:Bil.Kleb@nasa.gov]
Sent: Thursday, May 04, 2006 6:22
To: ruby-talk ML
Subject: Ruby equivalent to simple awk program

Hello,

I found this awk snippet in news:comp.lang.fortran this
morning, and it made me realize that I don't use Ruby as a
*nix filter as often as I probably should because I couldn't
duplicate the capability in "about 10 seconds":

  Glen Herrmannsfeldt wrote:

  Here is an awk program that will read in a file line
  by line and write the lines out in reverse order. It
  will allocate array elements as long as there is still
  available memory. Variables are initialized to zero
  (or ""), even array elements.

   { array[n++]=$0;}

   END {
      while(n-->0) print array[n];
      }

  This took about 10 seconds to write and worked the
  first time.

Later,
--
Bil
http://fun3d.larc.nasa.gov

Someone didn't test their code :wink: (It's ARGF not $ARGF)

···

On May 4, 2006, at 11:59 AM, Gennady Bystritsky wrote:

$ ruby -e 'puts $ARGF.readlines.reverse'

Gennady.

If you find yourself trying Perl before Ruby, however, seek
psychiatric help immediately :slight_smile:

Sometimes you'll find a machine that doesn't have ruby installed.

Besides, it's not that bad. Really I promise it won't hurt a bit.

    $ perl -ne 'push @a, $_; END { print reverse @a }'

It's not any worse than the awk (but not a pretty as the ruby). Like
it was said before, use the tools you have:

    $ tac test.rb

···

--
Lou

If you are going to resort to perl then you should probably use

$ perl -e 'print reverse <>'

Mike

···

On 4-May-06, at 4:14 PM, Louis J Scoras wrote:

If you find yourself trying Perl before Ruby, however, seek
psychiatric help immediately :slight_smile:

Sometimes you'll find a machine that doesn't have ruby installed.

Besides, it's not that bad. Really I promise it won't hurt a bit.

   $ perl -ne 'push @a, $_; END { print reverse @a }'

It's not any worse than the awk (but not a pretty as the ruby). Like
it was said before, use the tools you have:

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

If you are going to resort to perl then you should probably use

$ perl -e 'print reverse <>'

Heh. Very true, that's much better. I had awk on the brain.

···

--
Lou

Louis J Scoras wrote:

If you are going to resort to perl then you should probably use

$ perl -e 'print reverse <>'

Heh. Very true, that's much better. I had awk on the brain.

ruby -e '$><<[*$<].reverse'

This looks like Perl is supposed to look, too.

···

--
Florian Frank

ruby -e '$><<[*$<].reverse'

A great big +1 for that one :slight_smile: Just to prove that ruby can be every bit as ugly (shows flexibility, right?) as any other language :slight_smile:

tsuraan@tsuraan.net wrote:

ruby -e '$><<[*$<].reverse'

A great big +1 for that one :slight_smile: Just to prove that ruby can be every bit as ugly (shows flexibility, right?) as any other language :slight_smile:

I initially misread that as:

   ruby -e '$><<[*$<].perverse'

which seems somehow *far* more appropriate...

···

--
Alex