Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Finding hash key related to closest value

by hdb (Prior)
on Mar 12, 2014 at 17:54 UTC ( #1078059=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Finding hash key related to closest value
Select or Download Code
Replies are listed 'Best First'.
Re^2: Finding hash key related to closest value
by nanophd (Novice) on Mar 12, 2014 at 18:02 UTC
    That is absolutely brilliant, Thank you. It works great!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1078059]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2015-07-31 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls