Do you really need to split the lines into separate arrays to sort them? That is, do you want a separate sorted list of lastnames, a sorted list of firstnames, and so on? Or do you want to sort the lines by lastname and then firstname?

If you want the latter (I'm guessing you do), all you have to do is to make a custom sort routine. You can either leave the lines as they are (to save on memory), or split them (to save time, perhaps, and to make it easier to deal with the data later). The easiest to demonstrate is probably to split them first:

my @lines; while (<>) { chomp; my @line = split(','); push(@lines, \@line); } @lines = sort { $a->[0] cmp $b->[0] or $a->[1] cmp $b->[1] or $a->[2] <=> $b->[2] or $a->[3] <=> $b->[3] or $a->[4] <=> $b->[4] } @lines; foreach my $line (@lines) { print join(",", @{$line}) . "\n"; }
Here, we read each line, strip off line endings, and then split it on comma boundaries. A reference to the resultant array is then pushed into the array @lines. Then we sort @lines using a custom sort routine that does a sort by the fields in order. This assumes that the first two fields are alphabetical, and the rest numeric. I'm assuming that height is in some rational form like cm.

Then the program just prints out the sorted lines, with commas put back in. You can fiddle with the sort routine to make it case insensitive, etc. but the basic idea still stands.


In reply to Re: array splitting and sorting by bikeNomad
in thread array splitting and sorting by Anonymous Monk

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.