|
|
| Keep It Simple, Stupid | |
| PerlMonks |
Re: Reset while loop to beginning of hash when using 'each'by davido (Cardinal) |
| on Sep 25, 2003 at 19:23 UTC ( [id://294288]=note: print w/replies, xml ) | Need Help?? |
This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.
According to the perldoc for each you can reset the each iterator function in one of the following ways:
You might think that evaluating keys for the hash is an inefficient way of resetting the each iterator. It might be, if you were to evaluate keys in list context. Don't. Just evaluate keys in scalar context, which efficiently returns the number of keys, rather than the list of keys. After that, you'll find each has been reset as desired. Here's a sample illustration:
If you run that you'll now find yourself in a neverending loop, since each gets reset each time through the while loop. In this illustration, the use of the pseudo-function scalar is unnecessary. As long as keys isn't being evaluated in list context, Perl is smart enough to not go to the work of creating a list of keys that just gets thrown away. Hope this helps... Dave "If I had my life to do over again, I'd be a plumber." -- Albert Einstein
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||