I know implemented some general loop for your problem. A index array is used to remember which index of which array is currently taken. In every iteration the index of the first array is incremented and wrapped around to zero if got to big. In this case the second index is incremented, and so forth...

The resulting order is combinations is here in different, but this could be fixed by changing the order of index incrementation.

my @lists = ( [1,2,6], [4,5], [7,3], ); # Init indexes to zero my @idx = map { 0 } (0..$#lists); my $n = 0; COMP: while (1) { $n++; print "comp[$n] = ["; # Join all list elements given by index array print join ',', map { $lists[$_][$idx[$_]] } (0..$#lists); print "]\n"; # Increment lowest index, handle overrun INDEX: foreach my $i (0..$#lists) { $idx[$i]++; # If new index greater then highest index if ($idx[$i] > $#{$lists[$i]}) { # Wrap around $idx[$i] = 0; if ($i == $#lists) { # Stop outher loop if last index overruns last COMP; } } else { # stop if no overrun occured last INDEX; } } } __END__ comp[1] = [1,4,7] comp[2] = [2,4,7] comp[3] = [6,4,7] comp[4] = [1,5,7] comp[5] = [2,5,7] comp[6] = [6,5,7] comp[7] = [1,4,3] comp[8] = [2,4,3] comp[9] = [6,4,3] comp[10] = [1,5,3] comp[11] = [2,5,3] comp[12] = [6,5,3]
Update: To get the same order like in your example add/change the following:
22a23 > $i = $#lists - $i; 28c29 < if ($i == $#lists) { --- > if ($i == 0) {
e.g. add the first line after the INDEX foreach loop line and change the if statement like shown.

In reply to Re^2: combine elements of lists by mscharrer
in thread combine elements of lists by alkis

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.