Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Processing text file in reverse-order linewise

by tomazos (Deacon)
on Aug 14, 2006 at 22:07 UTC ( [id://567351]=perlquestion: print w/replies, xml ) Need Help??

tomazos has asked for the wisdom of the Perl Monks concerning the following question:

So I want to do this:

while (<FILE>) { ... do something with $_ ... }

...but rather than process the first line first, I want to process the last line first, and then process the second last line, and then the third last line, all the way to the first line.

Any ideas about how to do this elegantly and efficiently?

Replies are listed 'Best First'.
Re: Processing text file in reverse-order linewise
by andyford (Curate) on Aug 14, 2006 at 22:10 UTC
Re: Processing text file in reverse-order linewise
by ikegami (Patriarch) on Aug 14, 2006 at 23:33 UTC

    Tie::File reads the file from the front, memorizing the position of the lines within the file. Tie::File is therefore really fast at random access, but uses up a log of memory if you're just going to read the file from the end.

    File::ReadBackwards really does read the file from the end, using a minimal amount of memory.

Re: Processing text file in reverse-order linewise
by davidrw (Prior) on Aug 14, 2006 at 23:19 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://567351]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found