in reply to Pick any key from a very large hash
my %hash = ( a => 1, b => 2, c => 3, d => 4, e => 5, f => 6, g => 7, h => 8, i => 9, j => 10, ); my $hash_length = %hash; # To get actual length you would need 'scalar keys %hash' # But we are not using 'keys' here so lets get the length other way $hash_length =~ s{/.*}{}; foreach (1...10) { my $rand_num = rand $hash_length; # Make sure number is even. $rand_num = ($rand_num %2) ? ($rand_num - 1) : ($rand_num); my $key = (%hash)[$rand_num]; print "$_\t: $key\n"; } # output # 1 : e # 2 : d # 3 : c # 4 : j # 5 : a # 6 : e # 7 : e # 8 : c # 9 : j # 10 : e
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Pick any key from a very large hash
by GrandFather (Saint) on Jul 12, 2009 at 05:11 UTC | |
by ashish.kvarma (Monk) on Jul 12, 2009 at 10:53 UTC | |
Re^2: Pick any key from a very large hash
by FloydATC (Deacon) on Jul 12, 2009 at 06:50 UTC |