From: mathew
Date: 2005-07-20T00:00:55+09:00
Subject: Re: Reading total lines of file then displaying in reverse?
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.
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. ]
--
WE HAVE TACOS