Dear Monks,
I love this site and have learned more about PERL here, than any other website. I was hoping some one could help me sort some arrays by a field. I have tried to apply almost every example for sorting that I could find here on Perlmonks, but I can not get it to work correctly. Sometimes it return a hash, sometimes... well, I'm having a hard time. I have about 40000 thousand lines to sort, but I will only give you a sample of the data, that I suck into an array, from a text file.
I'm not woried about sorting field 0, all I want to do is sort field 4 and print out the results, from highest number to lowest.
open(INF,"look.txt") || print "Cannot open look.txt file\n";
@look = <INF>;
close(INF);
@out = sort { (split '|', $a, 12)[4] <=> (split '|', $b, 12)[4] } @loo
+k;
foreach $line (@out) {
@fields = @$line;
print "@fields[0],@fields[1],@fields[2],@fields[3],@fields[4],";
}
My data looks like this:
accred|143|0|0|412|0|0|0|0|0|0|0|0|
accu-b|36|0|0|103|0|38|0|0|0|0|2|0|
accua|35|0|0|27|0|37|0|0|0|0|1|0|
accur|173|0|0|486|0|0|0|0|0|0|0|0|
accura|43|0|0|1022|0|4|0|0|0|0|0|0|
accurat|215|0|0|730|0|233|0|0|0|3|0|0|
accweb|1|0|0|0|0|38|10|2|0|0|0|0|
acdev|0|0|0|0|0|3|0|0|0|0|0|0|
aceas|11|0|0|28|0|36|0|0|0|0|0|0|
acecok|127|0|0|58|0|37|0|0|0|0|0|1|
acegi|76|0|0|63|0|102|20|1|0|0|0|0|
aceliv|4|0|0|275|0|59|0|0|0|3|0|0|
ace130|31|0|0|93|0|19|0|0|0|0|0|0|
Thanks.
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.