Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

All array combinations in fixed order

New Here ,
Oct 23, 2015 Oct 23, 2015

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.

TOPICS
ActionScript
364
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 23, 2015 Oct 23, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 24, 2015 Oct 24, 2015
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines