Skip to main content
June 21, 2013
Question

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

  • June 21, 2013
  • 2 replies
  • 1765 views

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 has been closed for replies.

2 replies

Known Participant
June 23, 2013

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;

}

June 23, 2013

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

Known Participant
June 23, 2013

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 ",".

Ned Murphy
Legend
June 21, 2013

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.

June 21, 2013

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? 

Ned Murphy
Legend
June 21, 2013

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?