I think Statistics::Lite could help you, it's pure perl and easy to use.
Else, a simple sort algorithm should help you, if you have your data stored in a hash like
# untested...
%list = (
RUBYKAT=>15.97 ,
SAPER=>15.78 ,
PETDANCE=>15.5 ,
AMBS=>15.5 ,
BARBIE=>15.45 ,
);
sub sortDesc {
$list{$b} <=> $list{$a};
}
foreach $key (sort sortDesc (keys(%list))) {
print "$list{$key} \t $key \n";
}
but i don't think there is a specific cpan module available to this.
anyway, i would not use an array of arrays for this.
"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
| [reply] [Watch: Dir/Any] [d/l] |
EDIT: Oops, had to update code for people with identical scores, and people who weren't on previous listing.
use strict;
use warnings;
my (%lrank, $c, $s, $name, $score, $lrank, $lscore);
my @lrank = ('RUBYKAT','SAPER','PETDANCE','AMBS','BARBIE');
my @nrank = (
['PETDANCE',12.4],
['SAPER',15.78],
['AMBS',15.5],
['RUBYKAT',15.97],
['BARBIE',16.06],
['NEWBIE',12.4],
);
$lrank{$_} = $c++ for @lrank;
$s = '%' . (length($#nrank+1)+1) . 's';
@nrank = sort {@$b[1] <=> @$a[1] || @$a[0] cmp @$b[0]} @nrank;
$c = 0; $lscore = 0;
for (@nrank) {
($name, $score) = @$_;
if (exists $lrank{$name}) {
$lrank = $lrank{$name};
print $lrank > $c ? '^' : $lrank < $c ? 'v' : ' ';
}
else { print ' '; }
print sprintf $s, $lscore == $score ? '' : $c;
print ": $name\n";
$lscore = $score; $c++;
}
Note that people of the same score are currently sorted alphabetically. Could be you'd want them sorted according to first come, first served, in which case a more complicated algorithm would be necessary. | [reply] [Watch: Dir/Any] [d/l] |