in reply to Using File::ReadBackwards or equivalent on pre-existing file handle

If it is only many megabytes, just store the lines into an array and use the reverse function on the array to print them backward. Might not work if it is many gigabytes, but that's not what you asked.
  • Comment on Re: Using File::ReadBackwards or equivalent on pre-existing file handle
  • Download Code

Replies are listed 'Best First'.
Re^2: Using File::ReadBackwards or equivalent on pre-existing file handle
by HYanWong (Acolyte) on Jun 25, 2014 at 23:25 UTC

    p.s. am I better unshifting the lines then doing a print(FH) foreach (@arr); or pushing the lines then doing a print(FH) foreach (reverse @arr);? I suppose I'm asking, is pushing faster than unshifting (I assume yes), and does perl make a new array copy when it does a foreach (reverse ...), or is it clever enough just to iterate backwards over the array. I presume it's clever...

      To sure, profile & benchmark the variations.

       print ... for reverse @list will generate a new, complete list I think (currently cannot find the right B magic to produce the optree display supporting the claim).

      Difficult to say without testing. I think that slurping the file from the FH directly into an array will be slightly faster than pushing or unshifting it line by line. Then poping the lines to be printed might be faster than using reverse, but only a benchmark can really say. You might even get diffrent results with differents versions of Perl. Overall, the difference is likely to be small or almost unsignificant.
Re^2: Using File::ReadBackwards or equivalent on pre-existing file handle
by HYanWong (Acolyte) on Jun 25, 2014 at 23:02 UTC

    It's only a 70Mb text file, so saving in memory is probably OK actually. Cheers