Note: I have never used NestedLoops before reading this question, so take this with a grain of salt.

Having said that, and having read through the docs a bit, NestedLoops lets you do with a single loop what you'd otherwise use a chain of N foreach loops to do, i.e. collectively walk across an N-dimensional parameter space.

In your phone number example, N=7, and each dimension would be a digit in the whole number. To keep it simple I'm going to use a 3 digit number (N=3) instead.

So, instead of writing:

@A_options = @B_options = @C_options = qw(0 1 2 3 4 5 6 7 8 9); foreach my $A (@A_options) { foreach my $B (@B_options) { foreach my $C (@C_options) { print $A . $B . $C . ;\n"; } } }
you could write:
@A_options = @B_options = @C_options = qw(0 1 2 3 4 5 6 7 8 9); NestedLoops( [\@A_options,\@B_options,\@C_options], sub {print $_[0] . $_[1] . $_[2] . "\n";} );
The information about where you are in the N-dimensional loop is passed to the called subroutine as consecutive params under @_.

Good luck,
- Dynamo


In reply to Re: Telephone - Nested Loops by dynamo
in thread Telephone - Nested Loops by FFRANK

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.