[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

File.each can start from bottom of file?

Roger

6/8/2006 9:37:00 PM


Instead of iterating from first to last line in file,
can the each method go from last to first?

If no, any ideas to achieve this?

Thanks,
Roger

4 Answers

Dave Burt

6/8/2006 10:10:00 PM

0

Roger wrote:
> Instead of iterating from first to last line in file,
> can the each method go from last to first?
>
> If no, any ideas to achieve this?

If your file isn't huge, use IO.readlines(filename).reverse.each

Cheers,
Dave

Daniel Berger

6/8/2006 11:18:00 PM

0

Dave Burt wrote:
> Roger wrote:
> > Instead of iterating from first to last line in file,
> > can the each method go from last to first?
> >
> > If no, any ideas to achieve this?
>
> If your file isn't huge, use IO.readlines(filename).reverse.each

If using IO.readlines isn't viable due to file size, take a look at
this old thread:

http://tinyurl...

Regards,

Dan

Dave Burt

6/9/2006 1:43:00 AM

0

Daniel Berger wrote:
> Dave Burt wrote:
>> Roger wrote:
>>> Instead of iterating from first to last line in file,
>>> can the each method go from last to first?
>>>
>>> If no, any ideas to achieve this?
>> If your file isn't huge, use IO.readlines(filename).reverse.each
>
> If using IO.readlines isn't viable due to file size, take a look at
> this old thread:
>
> http://tinyurl...
>
> Regards,
>
> Dan
>

Also, there's this more recent implementation by JEGII:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

Cheers,
Dave

Robert Klemme

6/9/2006 6:52:00 AM

0

Dave Burt <dave@burt.id.au> wrote:
> Roger wrote:
>> Instead of iterating from first to last line in file,
>> can the each method go from last to first?
>>
>> If no, any ideas to achieve this?
>
> If your file isn't huge, use IO.readlines(filename).reverse.each

For efficiency reasons you can iterate in reverse without creating the
reversed array first:

IO.readlines(filename).reverse_each

And it is exactly the same amount of character typing. :-)

Kind regards

robert