Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Your problem requires the following
  • The number of loops
  • The iteration ranges for each loop
  • Code which uses the loop indices
You can encode the first two however you'd like; in the following example I've simply put the ranges in an array.

You haven't provided an example of the code which runs inside the innermost loop. It might be useful if you provided some idea of what you're trying to do inside of those loops.

I'm assuming that your code only needs to know the loop indices.

Here's how you can use Math::Cartesian::Product:

use Math::Cartesian::Product; # this encodes the number of loops and their ranges my @ranges = ( [ 0..1 ], [ 1..2 ], [2..3] ); # the code inside the braces is run for every combination # of the ranges cartesian { print "indices: @_\n"; } @ranges; # here's a version using a subroutine which sums up # the indices (just to do something with them) sub compute { my $sum = 0; $sum += $_ foreach @_; print "sum of @_ = $sum\n"; } cartesian \&compute, @ranges;
And here are the results:
indices: 0 1 2 indices: 0 1 3 indices: 0 2 2 indices: 0 2 3 indices: 1 1 2 indices: 1 1 3 indices: 1 2 2 indices: 1 2 3 sum of 0 1 2 = 3 sum of 0 1 3 = 4 sum of 0 2 2 = 4 sum of 0 2 3 = 5 sum of 1 1 2 = 4 sum of 1 1 3 = 5 sum of 1 2 2 = 5 sum of 1 2 3 = 6

In reply to Re^3: How to create loop in perl dynamically by djerius
in thread How to create loop in perl dynamically by adithi

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (8)
    As of 2014-10-24 10:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (131 votes), past polls