in reply to Re^2: Sorting and ranking
in thread Sorting and ranking
Of course. One of the beauties of perl, however, is the ability to easily switch over to using a hash.
The sort will have to be explicit, of course. instead of getting it as a freebie with the array.
As a bonus, you get the ability to properly handle negative and/or non-integer values.
#!/usr/bin/perl # http://perlmonks.org/?node_id=1207623 use strict; use warnings; my %data = ( 'car' => 180, 'motorcycle' => 150, 'skate' => 150, 'bird' => 120, ); my ($n, %rank) = 1; $rank{ $data{$_} + 0 } .= "$_\n" for sort keys %data; $n += print s/^/$n - /gmr for @rank{ sort { $b <=> $a } keys %rank };
|
|---|