in reply to "Scalar found where operator expected", references

Other monks have already pointed out the bug and offered suggestions. I just want to show that there are more than one way to do it in Perl, depending on, personal taste, I'd say.

# Implementation 1 - the mundane way sub weighted_hash { my $hashref = shift; my @weighted; for my $key (keys %$hashref) { for (1 .. $hashref->{$key}) { push @weighted, $key; } } rand_elt @weighted; } # Implementation 2 - the perl monk one-liner sub weighted_hash { my $hashref = shift; rand_elt map { my $key = $_; map { $key } 1 .. $hashref->{$key} } keys %$hashref; }