This works for me:
use strict;
my %db = (
'scores' => {
'user1' => 200,
'user2' => 190,
'user3' => 232,
'user4' => 187,
'user5' => 190
}
);
my %sorted_scores;
#
# Generate another hash structure where
# scores are used for keys and values
# hold list of users who got that score.
#
# hash is a wonderful thing ;-)
#
for (keys %{$db{'scores'}}) {
push @{$sorted_scores{$db{'scores'}{$_}}}, $_;
}
my $i = 1;
for my $score (sort {$b <=> $a} keys %sorted_scores) {
print "$i Place: ";
for (@{$sorted_scores{$score}}) {
print "$_ ($score), ";
}
print "\n";
$i++;
}
This code produces the following output:
1 Place: user3 (232),
2 Place: user1 (200),
3 Place: user2 (190), user5 (190),
4 Place: user4 (187),
The only remaining part is to remove excessive commas and also print numbers in a slightly different format... (adding 'st' to 1 and 'nd' to 2 etc.)
Cheers,
vladb.
|
"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
|