[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Reading total lines of file then displaying in reverse?

Tristan Knowles

7/18/2005 10:52:00 AM

How would I use File or IO to read all the lines of a
file, then display the contents in reverse?

Its for a simple log reading script, which I would
like to have read the total lines, then display with
the latest entries first, and show x amount of log
entries per page.

I'm currently just dumping the whole, or part of the
log with:

======
IO.readlines("my.log")[1..49].each_with_index do
|line, index|
======

I guess I would have to count all lines, then assign a
variable to the last number it counts, and then work
in reverse from this variable... hmmm. I'm not
sure:P

Any ideas?



___________________________________________________________
How much free photo storage do you get? Store your holiday
snaps for FREE with Yahoo! Photos http://uk.photos...


7 Answers

Stefan Holst

7/18/2005 11:27:00 AM

0

Tristan Knowles wrote:
> How would I use File or IO to read all the lines of a
> file, then display the contents in reverse?

something like this?

File.readlines("my.log").reverse.each{|l| puts l}

RY
Stefan



Tristan Knowles

7/18/2005 11:58:00 AM

0

Cool. That reverses the value of the lines ordered,
but still lists the line numbers from 0 -> x.

Anyway to reverse this also, x -> 0?


Thanks again.

--- Stefan Holst <mail@s-holst.de> wrote:

> Tristan Knowles wrote:
> > How would I use File or IO to read all the lines
> of a
> > file, then display the contents in reverse?
>
> something like this?
>
> File.readlines("my.log").reverse.each{|l| puts l}
>
> RY
> Stefan
>
>
>






___________________________________________________________
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger...


daz

7/18/2005 12:01:00 PM

0


Stefan Holst wrote:
> Tristan Knowles wrote:
> > How would I use File or IO to read all the lines of a
> > file, then display the contents in reverse?
>
> something like this?
>
> File.readlines("my.log").reverse.each{|l| puts l}
>
> RY
> Stefan
>

There's Array#reverse_each which would save an
unnecessary array reversal :)

File.readlines("my.log").reverse_each{|l| puts l}


daz



dblack

7/18/2005 12:02:00 PM

0

Tristan Knowles

7/18/2005 12:06:00 PM

0


--- "David A. Black" <dblack@wobblini.net> wrote:
> > File.readlines("my.log").reverse.each{|l| puts l}
>
> I believe that's identical to:
>
> puts File.readlines("my.log").reverse


For my case, it now looks like this:

lines = IO.readlines("my.log").reverse.each_with_index
do |line, index|


I'm trying to use .length somewhere now to count the
number of lines.

Tristan





___________________________________________________________
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger...


dblack

7/18/2005 12:15:00 PM

0

Ara.T.Howard

7/18/2005 1:01:00 PM

0