While NestedLoops doesn't handle iterators for the values to loop over, it seemed easy enough to me to implement one that does--minus error checking, code wrapping and other conveniences that NestedLoops provides.

Update: well, I only lost by 24 minutes. lol

use strict; use warnings; # setup some iterators my ($i, $j, $k, $l) = (0) x 4; my @iter = ( sub { return $i++ unless $i == 2; $i = 0; return }, sub { return $j++ unless $j == 3; $j = 0; return }, sub { return $k++ unless $k == 1; $k = 0; return }, sub { return $l++ unless $l == 2; $l = 0; return }, ); # build the "nested loop" for them my $while_iter = nl_for_iters(@iter); # use the nested iterators while ( my @values = $while_iter->() ) { print join(', ', @values ) . "\n"; } # now how to build nested loops from iterators sub nl_for_iters { my @iter = @_; my @value = (); my $ptr = 0; return sub { LOOP: { if ( !defined $value[$ptr] || $ptr == $#iter ) { if ( defined ($value[$ptr] = $iter[$ptr]->()) ) { $ptr += 1 if ($ptr < $#iter); return @value; } else { pop @value; $ptr -= 1; return if $ptr < 0; pop @value; redo LOOP; } } } # LOOP } } __END__ 0 0, 0 0, 0, 0 0, 0, 0, 0 0, 0, 0, 1 0, 1 0, 1, 0 0, 1, 0, 0 0, 1, 0, 1 0, 2 0, 2, 0 0, 2, 0, 0 0, 2, 0, 1 1 1, 0 1, 0, 0 1, 0, 0, 0 1, 0, 0, 1 1, 1 1, 1, 0 1, 1, 0, 0 1, 1, 0, 1 1, 2 1, 2, 0 1, 2, 0, 0 1, 2, 0, 1

--Solo

--
You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

In reply to Re: NestedLoops (Algorithm::Loops) and Iterators by Solo
in thread NestedLoops (Algorithm::Loops) and Iterators by mrborisguy

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.