[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Processing file backwards?



> Hello all,
> 
> What is the most straightforward to traverse through an ascii file 
> backwards, along the lines of:
> 
> while ($line = <FILE>)
> {
>       ...
> }
> 
> but of course in reverse?

How about going through the file once, storing the positions of the
beginnings of the lines, and then seek()ing to the positions?

Something like:

@lines = (0);
while (<FILE>) { push @lines, tell FILE }
$#lines--;
foreach $line (reverse @lines) {
  seek FILE, $line, 0;
  $_ = <FILE>;
  # ... code here ...
}

-- 
  Kevin Reid.      |         Macintosh.
   "I'm me."       |      Think different.

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch