Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

how to read a file in backwards?

by redss (Monk)
on Jan 27, 2005 at 23:43 UTC ( [id://425812]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!

How can I open up a text file and read the lines in one at a time, starting from the END?

Thanks!

Replies are listed 'Best First'.
Re: how to read a file in backwards?
by bart (Canon) on Jan 27, 2005 at 23:47 UTC
    File::ReadBackwards: "Read a file backwards by lines."

    Allegedly, it's been strongly tweaked to be as efficient as possible, under the circumstances, so it's very unlikely carelessly homegrown code will beat it.

Re: how to read a file in backwards?
by Ovid (Cardinal) on Jan 27, 2005 at 23:53 UTC
Re: how to read a file in backwards?
by ZlR (Chaplain) on Jan 28, 2005 at 11:51 UTC
    Hello,

    If it's just a small file you could load every line into an array and reverse the array :

    open FFF, "< toctoc.txt " ; my @slurp = (<FFF>) ; print reverse @slurp ;
    You can treat each line with a foreach over the array, but it may not fit your need if you really want to read one line at a time from the original file.

    ZlR

Re: how to read a file in backwards?
by ercparker (Hermit) on Jan 28, 2005 at 19:33 UTC
    my @lines = <FILE>; while (my $line = pop @lines) { # do something }
Re: how to read a file in backwards?
by Miguel (Friar) on Feb 18, 2005 at 16:30 UTC
    Another alternative:
    use strict; use IO::All; print io("file.txt")->backwards->getlines;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2025-07-09 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.