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:
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.# 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 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |