This means that "each" mechanism is not very good to use.

It depends: As described in each, every hash has an iterator that is per-hash. The code you showed in the root node doesn't work because both of the each calls access the same iterator. My solution fetches the list of keys once at the beginning of the for loop, which will use a bit more memory; I could have done this for both loops, but in this example I knew that the inner each would work because there's nothing else interfering with the iterator, so I kept it. keys does have the advantage that if you want to loop through the hash in a predictable order, you can just say sort keys %hash.

Also, note that if you want to create the Cartesian product of two sets of hash keys, there are plenty of modules that will do this, see the links in the section "See Also" in my module Algorithm::Odometer::Tiny.

use warnings; use strict; use Algorithm::Odometer::Tiny; my $h = {a=>1, b=>2, c=>3, d=>4}; my $odo = Algorithm::Odometer::Tiny ->new( [sort keys %$h], [sort keys %$h] ); while ( my @x = $odo->() ) { print "outer: $x[0], inner: $x[1]\n"; }

In reply to Re^3: Nested iterations throgh hash by haukex
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.