Eek! I can't believe that no one metioned DBD::CSV. I see two answers that are using different arrays to store the data - why not just use a 2-d array?
my (%fields,@records); @fields{qw(lastname firstname age height weight)} = (0..4); while(<DATA>) { chomp; my @row = split(/,/,$_); push @records, \@row; } __DATA__ Smith,Joe,40,76,175 Page,Betty,60,58,165 Nader,Ralph,55,63,190 Alexander,Sue,25,54,98 Saxon,Jackson,32,60,155
Now you can write a subroutine that will sort the 2-d array and you can use %fields to reference the field by a name rather than a number.

But your subroutine should be smart enought to know the when to do a numerical sort ({ $a <=> $b }) and when to do an 'ASCII' sort ( { $a cmp $b }). It should also be smart enought to sort ascending ($a before $b) and descending ($b before $a).

You could do that, or you could use DBD::CSV:

use DBI; use Text::CSV_XS; # or your other favorite CSV module my $CSV = new Text::CSV_XS; my $DBH = DBI->connect("DBI:CSV:"); $DBH->{RaiseError} = 1; $DBH->do(" create table info( lname char(20), fname char(20), age integer, height integer, weight integer ) "); # i am still new to DBD::CSV, i am sure there is a way # to eliminate this while loop, i'll be researching... # in the meantime, we can use Text::CSV_XS . . . while (<DATA>) { chomp; $CSV->parse($_) or next; my $sth = $DBH->prepare(" insert into info values(?,?,?,?,?) "); $sth->execute($CSV->fields); } my $sth = $DBH->selectall_arrayref(" select * from info order by lname desc, fname asc "); foreach (@$sth) { print join("\t", @$_), "\n"; } # very mandatory to drop the table explicitly # if you wish to run this program more than once $DBH->do("drop table info"); __DATA__ Smith,Joe,40,76,175 Page,Betty,60,58,165 Nader,Ralph,55,63,190 Alexander,Sue,25,54,98 Saxon,Jackson,32,60,155
this will produce:
Smith Jane 30 57 113 Smith Joe 40 76 175 Saxon Jackson 32 60 155 Page Betty 60 58 165 Nader Ralph 55 63 190 Alexander Sue 25 54 98

Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--

In reply to (jeffa) Re: array splitting and sorting by jeffa
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.