in reply to Using 'each' to get random (key, value) pair from hash returns "uninitialized value"

each visits each key in turn. There may still be keys in the hash, but there are no keys left to visit.

Before calling each(%hash) in scalar context, one needs to call keys(%hash) to reset the iterator.

Important: each does not return a random key. No assurances are made that there is no bias. "Random numbers are too important to leave to chance", but that's what you're doing.

  • Comment on Re: Using 'each' to get random (key, value) pair from hash returns "uninitialized value"
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Using 'each' to get random (key, value) pair from hash returns "uninitialized value"
by puterboy (Scribe) on Jan 28, 2011 at 21:11 UTC
    Yes - I realize it is anything but random, hence the quotes in my original post. For my purposes, it needn't be random and the idea of a conductor walking back and forth collecting tickets and eventually hitting all the seats (as long as I eat the 'undef' at the end and let him start again) works good enough for me. Actually, it probably works better since it gives some approximate sens of FIFO