in reply to Re: trouble with each and a hash of hashes
in thread trouble with each and a hash of hashes
So, I will go for a foreach loop instead.
As a solution, that will work fine--but it's not without it's downside.
Using while each, avoids a (possibly significant) overhead that for keys or for values impose: that of constructing a list. If your hash is small, and if your going to iterate most of the elements anyway, then that may be acceptable.
However, there is a better way: If you call keys on the hash(ref) before entering your while loop, it will reset the iterator and you will avoid the problems you detailed above. keys is specifically optimised for this purpose. From perlfunc:
As a side effect, calling keys() resets the HASH's internal iterator, see each. (In particular, calling keys() in void context resets the iterator with no other overhead.)
|
|---|