Ruby equivalent to simple awk program


#1

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,


#2

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

by line and write the lines out in reverse order. It
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.


#3

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

by line and write the lines out in reverse order. It
This took about 10 seconds to write and worked the
first time.

Piece of cake ;-)

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:


#4

On 5/4/06, Bil K. removed_email_address@domain.invalid 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


#5

On May 4, 2006, at 8:34 AM, Gregory S. 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 G. II


#6

On Thu, May 04, 2006 at 10:22:16PM +0900, Bil K. wrote:
} 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


#7

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

Very much appreciated!

Cheers,

Marco

James G. wrote:

On May 4, 2006, at 8:34 AM, Gregory S. 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 G. II


#8

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?

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 G. II


#9

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

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.


#10

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