in reply to Random hash element (low memory edition).
You could use something like this:
Code updated per ikegami's post below.
sub getRandomPairFromHash{ my $href = shift; my $nKeys = keys %$href; my $n = 1 + int rand $nKeys; my( $key, $val ); ( $key, $val ) = each %$href while $n--; return ( $key, $val ) }
It does work and doesn't increase the memory consumption, but fast it's not.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Random hash element (low memory edition).
by ikegami (Patriarch) on Jan 25, 2008 at 02:13 UTC |