in reply to Re: Use Perl's Sort to only sort certain lines in a file?
in thread Use Perl's Sort to only sort certain lines in a file?

True.   However, in the present case, it looks to me like the problem consists of processing a file that consists of “sections,” with different self-contained sorting requirements for each section.   Once the entirety of “any particular section” has been read-in to memory from the file, it can be processed in its entirety, written to the output file, and then completely forgotten.   So, an array of arrays ought to be unnecessary.

The file consists of (1) “lines that are section-headers,” and (2) “everything else,” which lines are to be interpreted as part of the preceding section (if any).   Processing of each (preceding ...) section begins when the next section-header is read, and once-again at the end of the file.   The sort-requirements of each section can easily be handled by (one or several) sort-comparison functions.   The processing in each case is to sort the array of accumulated lines appropriately, then spit-out the lines preceded by their appropriate section-header, then get ready to process the next section.   Only the lines from “the current section” need be retained in memory at any point.

Replies are listed 'Best First'.
Re^3: Use Perl's Sort to only sort certain lines in a file?
by Laurent_R (Canon) on Jan 03, 2015 at 10:26 UTC
    Yes, you're absolutely right, sundialsvc4, this can be done section by section (read one section, sort it, print it out, then proceed to the next section, etc.), and this is actually what I would most probably do in such a case, especially if the input file is large.