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;