Hi luxs,

Your problem stems from this:

The iterator used by each is attached to the hash or array, and is shared between all iteration operations applied to the same hash or array. Thus all uses of each on a single hash or array advance the same iterator location. All uses of each are also subject to having the iterator reset by any use of keys or values on the same hash or array, or by the hash (but not array) being referenced in list context. This makes each-based loops quite fragile

See https://perldoc.perl.org/functions/each.html

To demonstrate, if you use two different hashes - you'll see what I mean. Here's a revised version of your code to demonstrate

#!/usr/bin/perl use strict; use warnings FATAL => 'all'; use v5.10; # activate say feature my $h1 = {'a'=>1, 'b'=>2, 'c'=>3, 'd'=>4,}; my $h2 = {'v'=>6, 'x'=>7, 'y'=>8, 'z'=>9,}; while( my ($k1, $v1) = each(%$h1)) { say "external $k1 => $v1"; while( my ($k2, $v2) = each(%$h2)) { say "internal $k2 => $v2"; } # getchar to pause iteration after inner loop print "getchar to pause iteration after inner loop\n"; my $c = <>; }

Best of luck
Shadow


In reply to Re: Nested iterations throgh hash by shadowsong
in thread Nested iterations throgh 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.