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

Revered Monks:

One of my co-workers just asked me a very interesting question: Is there a way to process a file from the bottom up instead of from the top? In other words, is there something I can do so that the loop

while (<FILE>) { ## some processing }
will process the file beginning with the last line of the file?

Replies are listed 'Best First'.
Re: Read File Backword
by sweetblood (Prior) on Jul 27, 2004 at 19:40 UTC
      ... which is more efficient on large files than Tie::File.
        Given that Tie::File reads the whole file into an array whereas File::ReadBackwards just 'reads the file backwards', definately File::ReadBackwards is more efficient. I converted a KSH script to Perl, using File::ReadBackwards and got a 400x increase in speed.

        YMMV

        CC

        I should have mentioned that the KSH script was *not* badly written. It also parsed many files that were 300-500M in size, where the info it needed was typically in the last thousand lines.

Re: Read File Backword
by blue_cowdawg (Monsignor) on Jul 27, 2004 at 19:33 UTC

        s there a way to process a file from the bottom up instead of from the top?

    use Tie::File; my @ray=(); tie @ray,"Tie::File","myfile" or die $!; for(my $i=$#ray;$i>=0; $i--){ my $line=$ray[$i]; # do something with it... } untie @ray;
    Caveat:thoroughly untest!

      Thanks for the super quick response. Does this mean that all the file is read into memory before we can access
      $ray[$#ray]
      If this is the case for extremely large files 1GB, I may have a problem.

        No. I read the source code before. It only reads in lines when it is needed, besides it deletes lines from memory when the buffer overflows. Memory is not a concern for that module.