There have been a lot of good replies, but I'm going to recommend making things as generic as possible: the chances are, you're going to want to do something similar again later, but maybe with a different number of parameters, or maybe one or more parameter uses a different set of values. Thus, wrap it in a function where you can programmatically change those.

Note: with hardcoded loops, like you show, if you want to change the number of parameters, you need to copy/paste more nested loops (and fix the indentation). And if you want to vary 11 of the 13 parameters on one run, but only 5 of the 13 parameters on the next run, then you have to remove (or comment out) some of the levels of nesting. That is not easy for on-the-fly changes.

If all the parameters want to take on the same values, then BrowserUK's solution is excellent (I need to learn to better think in terms of true iterators and the Algorithm::Combinatorics for such problems). But if one or more of the parameters needs a different set of values, then it's not I don't see it being (updated phrasing) easily extensible.

When I see loops nested more than about 2 deep, especially when all the work is only in the deepest nest, and doubly especially when the iterators are themselves an array, I think of converting it to one external loop on an iterator index, and one internal loop to generate the parameter array. After the parameter array is generated, but inside the iterator loop, then do the processing.

So, combining those, here's a suggested implementation, where the action taken is just printing the results, either to STDOUT or to a file.

While thinking about it after I tested, I would actually add another parameter to the function, which expects a CODEREF, that way you could then run $function->(@p) in the place of the print-statement, and make it even more generic.


In reply to Re: Parallelization of multiple nested loops by pryrt
in thread Parallelization of multiple nested loops by biosub

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.