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

puts ARGF.readlines.reverse

At least the ruby version is more concise.

···

On May 4, 2006, at 9:22 AM, Bil Kleb wrote:

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

Piece of cake :wink:

    puts File.readlines('somefile').reverse

And waaaayyyy much more readable :slight_smile:

    BTW, it worked for me the _second_ time, because I initially wrote
"read_lines" instead of "readlines". I don't use that method very much :slight_smile:

···

On Thu, May 04, 2006 at 10:22:16PM +0900, Bil Kleb wrote:

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.

--
Esteban Manchado Velázquez <zoso@foton.es> - http://www.foton.es
EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es

} 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":
[...]
} { array[n++]=$0;}
}
} END {
} while(n-->0) print array[n];
} }
}
} This took about 10 seconds to write and worked the
} first time.

I have gotten pretty good with Ruby and I *can* duplicate this in about 10
seconds:

input = (ARGV.length>0) ? File.new(ARGV[0]) : $stdin
arr = []
input.each_line { |line| arr << line }
arr.reverse_each { |line| puts line }

That said, I'd be inclined to do it in awk. (Or I'd use tail -r on systems
that support it, but that doesn't seem to include the GNU tools on Linux.)
Ultimately, use the best tool for the job. If it's a short script in awk
and that's what comes to mind, use awk. If you find yourself jumping
through hoops working it out in awk, you'll probably have better success in
Ruby. If you find yourself trying Perl before Ruby, however, seek
psychiatric help immediately :slight_smile:

} Later,
} Bil
--Greg

···

On Thu, May 04, 2006 at 10:22:16PM +0900, Bil Kleb wrote:

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":

[snip]

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

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

puts ARGF.to_a.reverse

About 1 second to write (a bit longer to test :slight_smile:

Regards,
Sean

···

On 5/4/06, Bil Kleb <Bil.Kleb@nasa.gov> wrote:

I have gotten pretty good with Ruby and I *can* duplicate this in about 10
seconds:

input = (ARGV.length>0) ? File.new(ARGV[0]) : $stdin

If you change that to:

input = ARGF

You get the same behavior, but can accept multiple argument files on the command-line.

arr =
input.each_line { |line| arr << line }

Usually when you iterate and collect like this, it's a sign that you really want a different method (readlines() or to_a() in this case).

James Edward Gray II

···

On May 4, 2006, at 8:34 AM, Gregory Seidman wrote:

input = (ARGV.length>0) ? File.new(ARGV[0]) : $stdin
arr =
input.each_line { |line| arr << line }
arr.reverse_each { |line| puts line }

input.each_line { |line| arr.unshift line }

it saves an iterator

I keep finding myself iterating and collecting this way. Would you care
please to illustrate with an example?

Very much appreciated!

Cheers,

Marco

James Gray wrote:

···

On May 4, 2006, at 8:34 AM, Gregory Seidman wrote:

I have gotten pretty good with Ruby and I *can* duplicate this in
about 10
seconds:

input = (ARGV.length>0) ? File.new(ARGV[0]) : $stdin

If you change that to:

input = ARGF

You get the same behavior, but can accept multiple argument files on
the command-line.

arr =
input.each_line { |line| arr << line }

Usually when you iterate and collect like this, it's a sign that you
really want a different method (readlines() or to_a() in this case).

James Edward Gray II

--
Posted via http://www.ruby-forum.com/\.

Pretty much anything with an #each has a #to_a method.
irb(main):001:0> str = "Hello\nOne\nTwo\n"
=> "Hello\nOne\nTwo\n"
irb(main):002:0> a1 =
=>
irb(main):003:0> str.each { |line| a1 << line }
=> "Hello\nOne\nTwo\n"
irb(main):004:0> a1
=> ["Hello\n", "One\n", "Two\n"]
irb(main):005:0> a2 = str.to_a
=> ["Hello\n", "One\n", "Two\n"]
irb(main):006:0> a2
=> ["Hello\n", "One\n", "Two\n"]

As you can see, #to_a does exactly what your code did, with a lot less typing.

···

On May 4, 2006, at 10:16 AM, Marco Bottaro wrote:

I keep finding myself iterating and collecting this way. Would you care
please to illustrate with an example?

Very much appreciated!

Cheers,

Marco

Well:

   arr =
   whatever.each { |e| arr << e }

Is just a long way to say:

   whatever.to_a

for any Enumerable. Also, map() and inject() are generally other options for avoiding these kinds of constructs.

Show us an example of where you have used it and I bet we can help you improve it.

James Edward Gray II

···

On May 4, 2006, at 9:16 AM, Marco Bottaro wrote:

I keep finding myself iterating and collecting this way. Would you care
please to illustrate with an example?