XP is just a number | |
PerlMonks |
Re: Sorting an array of arrays by fieldby Hue-Bond (Priest) |
on Oct 15, 2006 at 02:25 UTC ( [id://578337]=note: print w/replies, xml ) | Need Help?? |
I love this site and have learned more about PERL here, than any other website Not much, I guess ;^). Please, it's "Perl" (mind the capitalization). @look = <INF>; It's better to use <DATA> for your code. That way, the data goes with the program and you don't have to provide it separately. @out = sort { (split '|', $a, 12)[4] <=> (split '|', $b, 12)[4] } @look; You need to quote the pipe: '\|'. @fields = @$line; $line is not an arrayref. The split you did only extracted the 4th field of the string for sorting, nothing more. @out is a regular array of strings, like @look. Applying this comments, and adding strict and warnings, your code becomes:
If you want to access the fields individually, you have to perform another split inside the foreach loop. Further on, doing a Guttman-Rosler transform, it could be:
Update: Added benchmark. || Included ambrus' solution. --
In Section
Seekers of Perl Wisdom
|
|