Having had bad experience with
Text::CSV_XS (I'm guessing it was probably me, not the module ;o) I tend to use hand-rolled solutions if it's nice'n'simple (I mean *really* simple), otherwise it's best to rely on the tried-and-tested APIs available. So anyway here's my meagre effort -
use strict;
$_ = <DATA>;
my $cnt = tr/,/,/ + 1;
my @fields;
push @fields, split /,/ while <DATA>;
chomp(@fields);
my %data;
push @{$data{$_ % $cnt}}, $fields[$_] for 0..@fields-1;
my @row;
for (keys %data) {
@row = sort @{$data{$_}};
print "$_ - @row\n";
}
__END__
num1,num2,num3
123,345,578
345,349,340
12,348,023
534,90283,230
239,394,283
What this does is
- counts the amount of fields (which in turn assumes theres a header line)
- stores all the data in a flat array
- get's rid of those danged newlines
- creates a hash with the columns as the keys
- then just loops through the data and sorts each column
Sorry if this is a little direct in approach, but if you're rolling your own it might nudge you in the right direction (and I was looking for a challenge, simple though it is :o)
HTH
broquaint
Update: it's probably best to go with davorg's approach if you actually want to implement this, but I'll leave this here in the spirit of TMTOWTDI.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.