Syntactic Confectionery Delight | |
PerlMonks |
Re: Random hash element (low memory edition).by Roy Johnson (Monsignor) |
on Jan 24, 2008 at 19:29 UTC ( [id://664125]=note: print w/replies, xml ) | Need Help?? |
You can find the number of elements using keys in a scalar context, and it won't take time or space to go through the elements.
But you could use the recipe for printing a random line from a file to just iterate through the hash without knowing how many elements it has and select a random one. Yes, as long as you don't change the hash, keys will return keys in the same order every time. values will return values in the corresponding order. Caution: Contents may have been coded under pressure.
In Section
Seekers of Perl Wisdom
|
|