Here's my uber-inelegant serialized approach... If you don't care about the order that every combination is created: (btw, I renamed @array to be @arrays)...
my ($carry, @tallykeeper) = map 0, @arrays, 0; #gets rid of warnings.. +. w/strictures while (!$carry) { $carry = 1; for my $dx (0 .. $#arrays ) { print $arrays[$dx]->[$tallykeeper[$dx]]; $tallykeeper[$dx] += $carry; if ($tallykeeper[$dx] < @{$arrays[$dx]}) { $carry = 0; }else{ $tallykeeper[$dx] = 0; } } print "\n"; }
OR if you MUST have it in that order you could reverse the order of your array lists, or with my solution use this solution:
my ($carry, @tallykeeper) = map 0, @arrays, 0; #gets rid of warnings.. +. while (!$carry) { $carry = 1; my $combostr = ""; for my $dx (reverse 0 .. $#arrays ) { $combostr .= $arrays[$dx]->[$tallykeeper[$dx]]; $tallykeeper[$dx] += $carry; if ($tallykeeper[$dx] < @{$arrays[$dx]}) { $carry = 0; }else{ $tallykeeper[$dx] = 0; } } $combostr = reverse $combostr; print "$combostr\n"; }

Okay mine are ultra lame... but it was fun to try to make a generic solution that worked for everything possible array of arrays...

--Ray


In reply to Re: Generating all possible combinations from an AoA by raybies
in thread Generating all possible combinations from an AoA by Anonymous Monk

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.