From: "Esteban Manchado Velázquez" Date: 2006-05-04T22:31:53+09:00 Subject: Re: Ruby equivalent to simple awk program --PpAOPzA3dXsRhoo+ Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, May 04, 2006 at 10:22:16PM +0900, Bil Kleb wrote: > Hello, >=20 > 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": >=20 > Glen Herrmannsfeldt wrote: >=20 > 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. >=20 > { array[n++]=3D$0;} >=20 > END { > while(n-->0) print array[n]; > } >=20 > 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 :-) --=20 Esteban Manchado Vel=E1zquez - http://www.foton.es EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es --PpAOPzA3dXsRhoo+ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) iD8DBQFEWgIrhYgK5b1UDsERAgs3AJ0S0KWMvfq5GyzNxMT/wm4j+CuizQCaAuu1 pxUWe47VEtKbaSPLm17h4UM= =K2Aw -----END PGP SIGNATURE----- --PpAOPzA3dXsRhoo+--