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

It seems to me that you need to build an array of arrays (AoA) in which you would slice each of your sections into a sub-array and then sort the sub-arrays individually.
  • Comment on Re: Use Perl's Sort to only sort certain lines in a file?

Replies are listed 'Best First'.
Re^2: Use Perl's Sort to only sort certain lines in a file?
by locked_user sundialsvc4 (Abbot) on Jan 02, 2015 at 19:46 UTC

    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.

      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.