1

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]?

3
  • It's really unclear what you are trying to achieve. What could your calculateDifference function be doing? You pass it two identical values... the difference between two numbers that are the same will certainly be 0...? Can you explain more about your overall goal. I fear you are overcomplicating a simple task. However, I have gone ahead and answered the question you asked even if it is probably the wrong question to be asking. Commented Mar 6, 2017 at 4:19
  • I have an if statement within the calculateDifference function that checks if a number is between 2 specific ranges and basically subtracts the difference between the 2 array items. Never thought about adding 5 different arrays into a single one and computing it the way you showed will have to try that out. Commented Mar 6, 2017 at 21:40
  • it's very common. It's called a 2D array if you want to look it up. Anytime you want to loop through multiple things that themselves have multiple things you want to also loop through. Commented Mar 6, 2017 at 22:46

3 Answers 3

1

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();
    }
}

update

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.