8.4. Reading a File Backwards by Line or ParagraphProblemYou want to process each line or paragraph of a text file in reverse. SolutionRead all lines into an array, then process that array from the end to the start: @lines = <FILE>; while ($line = pop @lines) { # do something with $line } Or store an array of lines in reverse order: @lines = reverse <FILE>; foreach $line (@lines) { # do something with $line } DiscussionThe limitations of file access mentioned in this chapter's Introduction prevent you from reading a line at a time starting from the end. You must read the lines into memory, then process them in reverse order. Needless to say, this requires at least as much available memory as the size of the file. The first technique moves through the array of lines, in reverse order. This destructively processes the array, popping an element off the end of the array each time through the loop. We could do it non-destructively with: for ($i = $#lines; $i != -1; $i--) { $line = $lines[$i]; }
The second approach generates an array of lines that is already in reverse order. This array can then be processed non-destructively. We get the reversed lines because the assignment to
These approaches are easily extended to paragraphs just by changing # this enclosing block keeps local $/ temporary { local $/ = ''; @paragraphs = reverse <FILE>; } foreach $paragraph (@paragraphs) { # do something } See Also
The Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|