Hi,
Its because hashes have only one internal iterator
my %hash = 1..4; my $stop = 0; while( my( $k, $v ) = each %hash ){ $stop++; print "# $k $v\n"; while( my( $kk, $vv ) = each %hash ){ print "## $kk $vv\n"; } die if $stop > 2; } __END__ # 1 2 ## 3 4 # 1 2 ## 3 4 # 1 2 ## 3 4 Died at - line 9.
First loop's each starts the iterator,
The second loop's each continues the iterator until the end is reached, when the iterator resets
then the first loop's each starts iterator again
Iterator can also be reset with keys
$ perl -le" %f=1..4; while( each %f ){print $g++; } " 0 1 $ perl -le" %f=1..4; while( each %f ){print $g++; keys %f; die if $g > + 5; } " 0 1 2 3 4 5 Died at -e line 1.
YAML::Syck::DumpFile uses keys/each in order to copy the hash, thus it ends up resetting the internal iterator
Maybe you want to dump the $v variable instead of the $hash? Or just dump the $hash outside of a loop?
In reply to Re: Action on each key/value pair in a hash
by beech
in thread Action on each key/value pair in a hash
by luxs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |