Skip to main content
Jun 21, 2013 1:01 AM UTC
Question

How would one go about searching an array for a combination of items?

I need to know how to search an array of numbers in Actionscript for a combination of numbers that equals a target number specified by the user. The combination could be any number of numbers. It could be two numbers, or three, or four, or more. The number of numbers in the array can vary. The user will manually populate the array. How could I do this? Any help is greatly appreciated.

This topic is closed to new replies. Start a new post to keep the conversation going.

12 replies

Known Participant
Jun 23, 2013 12:12 AM UTC

I think that something like this is what you are looking for... a is the big array and b is the array you want to find in array a.

var a:Array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

var b:Array = [5, 6, 7];

trace(arrayCombination(a, b));

function arrayCombination(array1:Array, array2:Array):Boolean

{

          var i:uint;

          for (i = 0; i < array1.length - array2.length; i++)

          {

                    if (array1 == array2[0])

                    {

                              var j:uint;

                              for (j = 1; j < array2.length; j++)

                              {

                                        if (array1[i + j] != array2)

                                        {

                                                  break;

                                        }

                                        if (j == array2.length - 1)

                                        {

                                                  return true;

                                        }

                              }

                    }

          }

          return false;

}

Translate
Jun 23, 2013 2:38 AM UTC

Thanks, GGamesStudio. How would this change if the arrays were populated by the user? What is the code for that?

Translate
Known Participant
Jun 23, 2013 2:54 AM UTC

Do you need the user to enter the combination?
If so, I think an Input TextField would do best.
Is that what you have in mind?
If so, then when the user is done entering the numbers a function would be run to place those values in the array.

For example if the user writes down 1,2,3,4,5   the array b will become [1, 2, 3, 4, 5]

So when you want to update the array:

b = arrayFiller("1, 2, 3, 4, 5, 6");

function arrayFiller(arg:String):Array

{

     var newArray:Array = new Array();

     var i;

     for (i = 0; i < arg.length; i++)

     {

          if (arg.charAt(i) != " " && arg.charAt(i) != ",")

          {

               newArray.push(int(arg.charAt(i)));

          }

     }

     return newArray;

}

trace(b);

I used "1, 2, 3, 4, 5, 6" in the example, replace that with what the user enters.

Also a little hint... Restrict the input of the textbox to only numbers and ",".

Translate
Ned Murphy
Legend
Jun 21, 2013 1:05 AM UTC

It is not clear to me what you ultimately intend for this searching, but one of the easiest ways to see if an array contains something is to use the indexOf() method of the Array class.

If it makes sense for what you intend, you could place the search values in an array and loop thru it to see if each of the items in the array matches the array you are searching thru.

Translate
Jun 21, 2013 1:45 AM UTC

Thanks, Ned Murphy. The biggest question that I have is how to search for any number of combinations that equal the target number. I know how to search for just one, but how do you search for any combination? 

Translate
Ned Murphy
Legend
Jun 21, 2013 1:50 AM UTC

I tried to answer that with my second paragraph...  put the combination in an array (or combinations) and loop thru them to see if the array contains them all.  What part of that do you not understand, or how does that not address what you are trying to do?

Translate