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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.