Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
If you could give an actual but simplified example...
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
Thanks, GGamesStudio. How would this change if the arrays were populated by the user? What is the code for that?
Copy link to clipboard
Copied
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 ",".
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now