Reputation: 13
I do not know what you would call or define this type of issue with AS3. I have 2 For Loops that look like this:
// Here I would have 5 arrays naming from NDW1 to NDW5
var NDW1:Array = new Array();
for(var a:int = 1; a < 6; a++){
for(var b:int = 0; b < 14; b++){
// Here I want to call a function and assign it to a textfield
this["DifferenceW"+a+"_"+b].text = CalculateDifference(Number("NDW"+a+"["+b+"]"), Number("NDW"+a+"["+b+"]")).toString();
}
}
My problem when I am trying to access the actual array of NDW while using counters to increment that actual array and array element.
But when I type "NDW"+a+"["+b+"]" it will only return a string value. How do I actually get it to return the array with that current element?
So for example if NDW1[0] = 2
instead of just a string of NDW1[0]
?
Upvotes: 1
Views: 260
Reputation: 121
As others have already said, it's not very clear what you're trying to do and it looks like you're really over-complicating something that should be very simple.
To answer your question though, with "NDW"+a+"["+b+"]", your mistake is in thinking that you can concatenate a string to create a variable name. You can't. That will always return a string because it is a string that you are creating.
You could access it by using this["NDW"+a+"["+b+"]"] which evaluates to a property called "NDW"+a+"["+b+"]" on 'this' object, but that really isn't a very nice thing to do.
Neal's solutions look good but don't evaluate the length of the arrays on each iteration as that's very inefficient.
Upvotes: 0
Reputation: 2008
5 arrays and you want to loop through each array? Put those 5 arrays in 1 array.
private var _allArr:Array = [NDW1, NDW2, NDW3, NDW4, NDW5];
Now you can easily access everything without silly string concatenation.
for each (var a:Array in _allArr){
for each (var b:Number in a){
this["DifferenceW"+a+"_"+b].text = (CalculateDifference(a[b], a[b])).toString();
}
}
What does CalculateDifference
do? You are feeding it two identical numbers so I hope it isn't calculating the difference between those numbers (will always be 0).
Anyway. The other way to loop through them is this:
for (var i:int = 0; i < _allArr.length; i++){
for (var j:int = 0; j < _allArr[i].length; j++){
this["DifferenceW"+a+"_"+b].text = (CalculateDifference(_allArr[i][j], _allArr[i][j])).toString();
}
}
I'm going to guess that you want to find the difference between NDW1[0] and NDW2[0], etc. If so, your CalculateDifference function should be passed those two values. This is a bit trickier. Do this:
for (var i:int = 0; i < _allArr.length; i++){
for (var j:int = 0; j < _allArr[i].length - 1; j++){
this["DifferenceW"+a+"_"+b].text = (CalculateDifference(_allArr[i][j], _allArr[i+1][j])).toString();
}
}
Upvotes: 1
Reputation: 13
After trying multiple combinations with the keyword this
it can be used to access variables that you created which I never knew, but for future reference to access any variable you would use:
this["variableName"]
in my case for the for loop to access the elements you would use
this["NDW"+a][b]
Upvotes: 0