in reply to Re: easy way to get random key from hash?
in thread easy way to get random key from hash?
my ($i, $jokekey); ++$i && rand($i) < 1 && ($jokekey = $_) for (keys %hash);
Less readable than your sub, but same idea.
However, I wouldn't use it this way in production. Like you said, it always iterates through all the keys of the hash before it returns the random key. The benefit you gain by not needing to know how many elements isn't really a benefit. The following snippet will give you the same benefit and it's easier to read, and (probably) more efficient:sub choose { #assumes 1st param is \%knockknocks my $jokeref = shift; my @setups = keys %$jokeref; return $setups[rand @setups]; }
Now if the jokes are kept in a separate file, that could change everything ;-)
|
|---|