Your examples appear to be sorted in reverse numerical order, rather than by column 9 as you state. Coincidentally, with this structure, that is the same as a reverse dictionary sort on the strings. You can save a lot of memory by storing them as strings instead of splitting them.

There are still a couple of ways to go with it. You can either (A.)read the file as an array of lines and do a custom sort, or else you can (B.)build an array of arrays indexed by the first digit and sorted by the remainder:

# data file is open to read on the FOO handle my @lines = <FOO>; # option A. my @lines = sort { substr( $a, 0, 1) cmp substr( $b, 0, 1) or substr( $b, 1) cmp substr( $a, 1) } @lines; # option B. my @ary; push @{$ary[ substr( $_, 0, 1)]}, substr( $_, 1) for @lines; @ary = map {[ reverse sort @$_ ]} @ary;
If you don't need fast selection grouped on the first digit, option A is the pick. it will be lighter on memory, and possibly faster.

If you need the row data as an array, split will give you that as needed.

Update: Re: your followup That would be option B. @{$ary[4]} is the sorted array of all entries starting with four. The four has been stripped for convenience in sorting, but since you know the index you're using, you can restore it for printing or whatever.

After Compline,
Zaxo


In reply to Re: How can I read multiple lines starting with the same number and put in to a nested array and print it to a file? by Zaxo
in thread How can I read multiple lines starting with the same number and put in to a nested array and print it to a file? by opolat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.