in reply to looking for a module to handle 'highscore' lists
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.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++; }
|
|---|