use List::Util qw(first); my $i = 0; my @sortedarray = map {[$_,$i++]} sort keys(%hash); my $nr = (first {$_->[0] >= $test} @sortedarray)->[1];