Forum: Ruby Ruby equivalent to simple awk program

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bil K. (Guest)
on 2006-05-04 17:24
(Received via mailing list)
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,
Logan C. (Guest)
on 2006-05-04 17:30
(Received via mailing list)
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.
Esteban Manchado =?iso-8859-1?Q?Vel=E1zquez?= (Guest)
on 2006-05-04 17:33
(Received via mailing list)
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 :-)

    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
:-)
Gregory S. (Guest)
on 2006-05-04 17:36
(Received via mailing list)
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 :)

} Later,
} Bil
--Greg
Sean O. (Guest)
on 2006-05-04 17:42
(Received via mailing list)
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 :)

Regards,
Sean
James G. (Guest)
on 2006-05-04 17:49
(Received via mailing list)
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
Marco Bottaro (Guest)
on 2006-05-04 18:15
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
Logan C. (Guest)
on 2006-05-04 18:34
(Received via mailing list)
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.
James G. (Guest)
on 2006-05-04 18:47
(Received via mailing list)
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
David C. (Guest)
on 2006-05-05 02:24
(Received via mailing list)
>
> 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
This topic is locked and can not be replied to.