Easily extending foreach to multiple iterator-vars is a problem in perl5.

That's why the for syntax was extended in perl6

for %hash.kv -> my $key, $val { say "$key corresponds to $val"; }

Me myself I would love to have a real macro mechanism to solve this properly in perl5, anyway I can suggest you some code to simulate it at least in a functional way

use strict; use warnings; { my %lists; sub deal { my $aref=shift; my $id= \$_[0]; #print $aref,$id; $lists{$id}=$aref if (! exists $lists{$id} ); if (@{$lists{$id}}){ for(@_) { $_=shift @{$lists{$id}}; } return 1; # could be extended to # * iteration counter # * remaining list-elements # * a list of all of these } else { delete $lists{$id}; return 0; } } } $\="\n"; #--------- here we go while (deal [1..9] => my ($a,$b,$c) ){ print $a,$b,$c; while (deal [a=>1,b=>2] => my ($a,$b) ){ print $a,$b; } } #--------- print "EXIT"; __DATA__ 123 a1 b2 456 a1 b2 789 a1 b2 EXIT

I'm not sure if it works prior to 5.10, IMHO there was an issue about "late" my declarations.

Some things to note:

  1. The anonymous array will be build each time, but ignored after the first iteration.
  2. my first guess was to call the function "iter", but "deal" (like to deal cards to players) should make clear that it's not a lazy evaluated iterator-function!
  3. the reference of the first iterator-variable (here \$a) is taken as identifier to avoid confusion when nesting iterations, like with $_ when foreaching you should never mess around with the same instance of $a.
  4. you are flexible to use as many iterator-vars as you want, not only two. Taking one simulates foreach, with the advantage that gotos into the body are possible.
  5. the fat comma => is just syntactic sugar, but I think its more readable like this (and even a little more intuitive than ikegamis suggestions).
  6. the performance problem with rebuilding the list at each pass may be solved by passing a code-block,  sub {LIST} which is only once evaluated internally. When using prototype (&@) even the sub could be omitted².

Other approaches¹ solving the repeated LIST creation may be to split functionality onto two functions deal and to such that either

 for (deal(LIST);to(VARS)) { ... }

or

 while ( deal(LIST) .. to(VARS) ) { ... }

work! (But error-handling becomes trickier)

In the latter correct return values would make the flip-flop operator (scalar ..) build the list only once!

Cheers Rolf

UPDATES:

¹) thats really complicated to realize avoiding a nesting mess, because it's not possible to identify from which codeposition to() is called, the linenumber in caller is not sufficent.

²) maybe the best solution! The call syntax for iterating over a literal hash would be

while( deal {a=>1,b=>2} my($k,$v) ) {..}
Please note, no (fat) comma needed, because now we are passing code returning a list NOT a hashref! That's analogous to map and grep's syntax.

In reply to Re: Iterating over verbatim hash reference by LanX
in thread Iterating over verbatim hash reference by rovf

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.