Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How to randomly select a key whose value is between a range?

by golux (Chaplain)
on Sep 04, 2015 at 22:49 UTC ( [id://1141067]=note: print w/replies, xml ) Need Help??


in reply to How to randomly select a key whose value is between a range?

Construct the list of keys which have values within your desired range. Pull a random key out of that list, and you're done!
#!/usr/bin/perl # Libraries use strict; use warnings; # User-defined my $min = 2; my $max = 5; my %hash = ( a => 00, b => 01, c => 02, d => 03, e => 04, f => 05, g => 06 ); # Main program my @match = grep { $hash{$_} >= $min and $hash{$_} <= $max } keys %has +h; my $rand = $match[rand @match]; printf "Random key = '%s', Value = '%s'\n", $rand, $hash{$rand};
say  substr+lc crypt(qw $i3 SI$),4,5

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found