in reply to Speed up my code

I assume your input file is much biggger in reality otherwise I can't imagine this script taking a lot of time. In that case sorting the whole %query hash to only get the 5 highest might be slow

Instead you could use this:

my @result=([0,''],[0,''],[0,''],[0,''],[0,'']); foreach (keys %query) { my $sw; my $n= [$query{$_},$_]; if ($n->[0]>$result[4]) { if ($n->[0]>$result[3]) { if ($n->[0]>$result[2]) { if ($n->[0]>$result[1]) { if ($n->[0]>$result[0]) { $sw=$result[0]; $result[0]=$n; $n=$sw; } $sw=$result[1]; $result[1]=$n; $n=$sw; } $sw=$result[2]; $result[2]=$n; $n=$sw; } $sw=$result[3]; $result[3]=$n; $n=$sw; } $sw=$result[4]; $result[4]=$n; $n=$sw; } } print join("\n",map{$_->[1]},@result);

The speedup of this somewhat ugly contraption lies in the expectation that the first 'if' will be true very very seldom for a big hash