From the documentation for each:
After each has returned all entries from the hash or array, the next call to each returns the empty list in list context and undef in scalar context. The next call following that one restarts iteration.
The outer while loop grabs the only element in %test. The inner while loop gets an empty list, and consequently never executes. So the outer while loop tests each again. This time it has reset, so it again returns the first (and only) element from %test.
Nothing wrong with Perl. Your script is flawed.
Also, if you were using use strict; at the top of your script you would have discovered that print $name1 is not working out so well, since there is no $name1. The outer loop creates $name, not $name1. Furthermore, your code won't even compile as your outer while loop's parens don't nest properly.
You may want something more like this:
use strict; use warnings; my %test = ('a' => {'b' => 'b'}); while ( my ( $name, $value ) = each %test ) { print $name; while ( my ( $name2, $value2 ) = each %{ $value } ) { print $name2; } }
Dave
In reply to Re: Nested while each over a hash -> infinite loop
by davido
in thread Nested while each over a hash -> infinite loop
by Eythil
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |