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

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

Guest
Jun 20, 2013 Jun 20, 2013

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.

TOPICS
ActionScript
1.6K
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
LEGEND ,
Jun 20, 2013 Jun 20, 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.

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
Guest
Jun 20, 2013 Jun 20, 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? 

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
LEGEND ,
Jun 20, 2013 Jun 20, 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?

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
Guest
Jun 20, 2013 Jun 20, 2013

Oh I see what you mean. I'm not sure I know the Actionscript syntax for putting the combinations in the array, especially since the array can be of any size. Can you explain? I really appreciate the help, by the way.

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
LEGEND ,
Jun 20, 2013 Jun 20, 2013

I might be able to provide an example if you can explain the situation a little better.  If you could give an actual but simplified example it would be easier to show an approach.  Otherwise, I cannot be sure I am addressing the combinations aspect the way you expect it needs to be.

The size of an array for what I am suggesting is really of no consequence.  When you loop thru an array you don't need to know how big it is ahead of the process.  You can add all the items, or groups of items (multi-dimensional arrays) to an array as well and you still do not need to know how large it is... you just use its length property to control how far you go.

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
Guest
Jun 20, 2013 Jun 20, 2013

I need to know how to specify the combinations in the array. If the for loop is this: for (var i:int=0; i <array.length; i++), are the combinations specified using array and array[i+i]? I'm just not sure how to tell the biprogram to search the specific combinations... Does this make any sense?

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
LEGEND ,
Jun 21, 2013 Jun 21, 2013

If you could give an actual but simplified example...

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
Explorer ,
Jun 22, 2013 Jun 22, 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;

}

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
Guest
Jun 22, 2013 Jun 22, 2013

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

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
Explorer ,
Jun 22, 2013 Jun 22, 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 ",".

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
Guest
Jun 22, 2013 Jun 22, 2013

Yes! That's excellent! Thanks a lot! One more question: What is the best way to grab user input in Actionscript? I know how to do it easily in Java, but I'm new to Actionscript. I just need to know the syntax for it.

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
Explorer ,
Jun 23, 2013 Jun 23, 2013
LATEST

Hey, user input in Actionscript is a bit different than what you may be used from the output panel/console from other languages...
The trace statement is used to print something to the output panel... There is no function to receive input...

So the only easy way for the user to input text is through a text field placed on the stage. Create a text field on the stage, change the type to Input Text Field and give it an instance name. You can then refer to its text using its text property like so: textFieldName.text.

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