Update: I thought this was an original idea. Instead of using the other methods everyone else was suggestion I attempted (succesfuly i might add) to create a hash with the column to be sorted as the key, and the whole string as the value for the key. Sort the hash keys and you're done (probably). Oh well, I guess it wan't novel enough (someone didn't like it).
--------end of update--------------
Being that you are using this to learn, I will stick mostly to the theory (of how I would do it).
I found this to be one novel way attack the problem.
I would start by creating a hash with the keys of the hash equivalent to the values of the column you are sorting on.
Use the desired field for the hash key (the ".=" covers multiple keys of the same name (tho not sorting them later))
foreach my $line (@file) {
chop ($line);
$line =~ s/ //g;
my @values = split (/,/, $line);
$thehash{$values[$field]} .= "$line\n";
}
Once the hash is created, sort it and print it:
@keys = sort(keys %thehash);
foreach (@keys) {
print "$thehash{$_}";
}
This will cover having two identical key values, but
will not sort them appropriately (at least in my mind)
e.x.- (sorted on first field, it doesn't look to the second field for more clarification)
99,bbbc,5fa3aaa
99,ccc,2143
It will only sort the actual field specified.
I leave fixing that to the reader.
Then again you could just put the data into a database and use SQL.
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.