Skip to main content
CreatoX-hsgIcE
Participant
October 23, 2015
Question

All array combinations in fixed order

  • October 23, 2015
  • 1 reply
  • 394 views

Hi there,

Gotten stuck on a relative smple question.

This is my multidimensional example Array:


var test:Array = [[0,1],[1,0],[2],[3],[4,5]];

For every combination in this fixed order I want the output. So the output for this example is:

01234

01235

11234

11235

00234

00235

10234

10235

I found this article, but cannot get it working in ActonScript: LINK

In the above example the Array has 5 elements (including nested arrays). But it also should work with arrays with more ore less items.

Hope someone can help me figure out how to achieve this.

This topic has been closed for replies.

1 reply

Inspiring
October 23, 2015

Take a look at this link Subset of combinations of an array

CreatoX-hsgIcE
Participant
October 24, 2015

Thank you for the reply.

Not getting the correct results. The script generates all possible combinations. I just need the combinations in the order of the array.

Below script is exactly what it must do. It gives every combination in the order of the array.But the script is not very well written and not flexible.

When adding or removing an item in the test Array, it still should work.

var test:Array = [[0,1],[1,0],[2],[3],[4,5]];

var aa:int,bb:int,cc:int,dd:int,ee:int;

for each (aa in test[0]) {

     for each (bb in test[1]) {

          for each (cc in test[2]) {

               for each (dd in test[3]) {

                    for each (ee in test[4]) {

                         trace(aa+","+bb+","+cc+","+dd+","+ee);

                    }

               }

          }

     }

}

/*

Results:

0,1,2,3,4

0,1,2,3,5

0,0,2,3,4

0,0,2,3,5

1,1,2,3,4

1,1,2,3,5

1,0,2,3,4

1,0,2,3,5

*/

I hope someone can help me creating a function for the above for each commands.