# An inline loop is more efficient then a block - thought almost imperceptable $KP->{ $_->{k} }{ $_->{v} }++ while $kills_sth->fetchrow_hashref(); # Before I did not know if it was safe to assume no negative numbers - now I can my %top = map { $_ => {'score' => 0, 'pid' => '', 'tid' => ''} } 1 .. 10; my @items = qw(score pid tid); for my $pid ( keys %{ $KP } ) { SCORE: for my $tid ( keys %{ $KP->{$pid} } ) { my $score = $KP->{$pid}{$tid}; next if $score < $top{10}{score}; # May or may not improve efficiency for my $rank ( 1 .. 10 ) { if ( $score > $top{$rank}{score} ) { (@{$top{$rank}}{@items}, @{$top{$rank + 1}}{@items}) = ($score, $pid, $tid, @{$top{$rank}}{@items}); next SCORE; } } } } for my $rank ( 1 .. 10 ) { printf("%.2d. f: %s\tp: %s\tt: %s\n", $rank, @top{@items}); }