http://www.perlmonks.org?node_id=1078059


in reply to Finding hash key related to closest value

Assuming that performance is not your main concern and that your data really is bell-shaped (in particular increasing up to the max, and then decreasing), the following should do the trick:

my $max = max values %hash; my $half = $max/2; my @above = grep { $hash{$_} >= $half } keys %hash; my $keylo = min @above; my $keyhi = max @above; print "1: $max\n"; print "2: $half\n"; print "3: $keylo, $keyhi\n";

If you need the closest, you might want to check the neighbors of $keylo and $keyhi as well.