0

What I need is to loop through unknown number of arrays of unknown length. Assume I have this data

my @aAry = qw ( a b c );
my @bAry = qw ( d );
my @cAry = qw ( e f g h );

There may be more. what I would do for a know quantity is :

for (my $i = 0; $i < scalar(@aAry); $i++) {
  for (my $j = 0; $j < scalar(@bAry); $j++) {
    for (my $k = 0; $k < scalar(@cAry); $k++) {
      $str = $aAry[$i].'.'.$bAry[$j].'.'.$cAry[$k];
      print "${str}\n";
    }
  }
}

The results would be every possible combination, 3 wide, of every item in each list.

a.d.e , a.d.f, a.d.g, a.d.h, b.d.e, b.d.f, b.d.g, b.d.h, c.d.e, c.d.f, c.d.g, c.d.h

3 wide(3 arrays) * 4(widest array) = 12 combinations There may be from 2 to 10 arrays that may be a width from 1 to 200 wide and I need to generate all possible combinations across N arrays that are M wide I'm thinking that I may be able to put them all into a single multi-dimensional array and just skip

my @mdARY = ( qw (a b c)
            , qw ( d )
            , qw ( e )
            );
for ($i = 0; $i < scalar(@mdARY); $i++) {
  for ($j = 0; $j < scalar(@{$mdARY[$i]}); $j++) {

But this ain't gonna work either. Maybe recursion???

3
  • Unless you need to know your current position in the array, just use for my $i (@array); $i will hold the value. Commented Jul 26, 2018 at 16:39
  • 1
    Math::Cartesian::Product and Set::Product Commented Jul 26, 2018 at 16:48
  • 3 wide(3 arrays) * 4(widest array) = 12 combinations that isn't how it works; it is 3 (width of first array) * 1 (width of second array) * 4 (width of third array). so if you have 10 arrays that are 200 wide, it isn't going to be 10 * 200 combinations, it is going to be 200 ** 10 (100 sextillion arrays, way more than you could even possibly have memory for) Commented Jul 26, 2018 at 22:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.