While it would be possible to loop over two (or more) hashes of equal size at the same time (see example below), the question is why? The 'order' of the key-value pairs of the hashes will not directly correspond to each other in any meaningful way (as shown below) unless, IIRC, the keys of the pairs and the insertion order of the pairs are identical for the hashes – in which case, why not just use a single hash with an array of two (or more) values per key?

>perl -wMstrict -le "my %ha = qw(ka1 va1 ka2 va2 ka3 va3); my %hb = qw(bk1 bv1 bk2 bv2 bk3 bv3); while (my ($ka, $va) = each %ha and my ($kb, $vb) = each %hb ) { print qq{a: $ka => $va b: $kb => $vb} } " a: ka1 => va1 b: bk3 => bv3 a: ka3 => va3 b: bk2 => bv2 a: ka2 => va2 b: bk1 => bv1
Maybe you want to extract the sets of keys from two hashes, order the sets relative to each other in some way, and then process the key-value pairs?


In reply to Re: iterate/traverse Two Hashes at once by AnomalousMonk
in thread iterate/traverse Two Hashes at once by cthar

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.