in reply to selecting random key from hash
That'll get you the item without ever having built the entire list of keys in memory.my %hash = (populated somehow); my ($victim_key, $victim_value); for (0..int rand (scalar keys %hash)) { ($victim_key, $victim_value) = each %hash; } scalar keys %hash; # reset the iterator
But if that's not what you are optimizing for, you should clarify your question. If it's simply "don't use an array", then I call homework on the question (usually signalled by a bizarre "don't use X" where "X" is the most efficient way to do something).
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: selecting random key from hash
by Fletch (Bishop) on Jul 13, 2004 at 03:12 UTC | |
by merlyn (Sage) on Jul 13, 2004 at 03:20 UTC | |
|
Re^2: selecting random key from hash
by davidj (Priest) on Jul 13, 2004 at 04:12 UTC |