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.yahoo.com
catch('done') do
n_pages.times do
page_size.times do
throw 'done' if lines.empty?
print lines.pop
end
puts page_separator
end
end
harp:~ > ruby a.rb a.txt
10
9
8
7
6
···
On Mon, 18 Jul 2005, Tristan Knowles wrote:
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
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.
Given the size of log files and the expense of reversing arrays, I'd be inclined to index the thing. Define a class to wrap access to the file, and make it store an array of file offsets for the starts of the lines of the log file. It can keep an internal timestamp to know when the log file has changed and the array needs to have more entries appended to it; and skipping to the end and reading lines and adding the entries will be a very cheap operation.
Then you define dump_log(numlines, offsetfromend) to seek to offsetarray[offsetarray.length - offsetfromend], read in numlines to an array, and dump it out using reverse_each.
Even if you have to marshal your index data between calls to your program, that should still be faster than trawling through (say) my 12,484 lines of CUPS error log every time the user hits "next page"...
mathew
[ Call me old, but I don't like the 'A few MB is nothing, just slurp in
the whole log file' approach. ]
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.yahoo.com
___________________________________________________________
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com