in reply to [untitled node, ID 178427]
instead. Actually, scratch that, you want push @referrers, [ $element, $name, $count ];$referrers[$cnt] = [ $element, $name, $count ]; $cnt++;
Avoid index variables in Perl whenever possible (which is very nearly always due to push, pop, shift, unshift, splice and foreach) - they are a source of "one off" mistakes. Plus, the array operators are more efficient.
Anyway, using anonymous arrays, sorting becomes trivial: my @sorted_referrers = sort { $a->[2] cmp $b->[2] } @referrers; For the record, here's a simple version that will do this using your code and split: my @sorted_referrers = sort { (split("|", $a))[2] cmp (split("|",$b))[2] } @referrers; The following is a much more efficient way to do it:But honestly, it is nonsense to jump through all these hoops to work with stringified data when you have it available in native form to begin with.my @sorted_referrers = map { $_->[1] } sort { $a->[0] cmp $b->[0] }, map { [ (split("|"))[2], $_ ] } @referrers;
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
178434
by Samn (Monk) on Jun 30, 2002 at 23:18 UTC | |
by Aristotle (Chancellor) on Jun 30, 2002 at 23:25 UTC |