- Home /
[Solved]Using multiple arrays in one statement?
Hello. I have two arrays(firstArray & secondArray), and what I want to do is go through all of the variables inside them through a for loop function, with the help of another array(arrayIndex), just to preserve the code size.
var arrayIndex = new Array("firstArray","secondArray");
var firstArray = new Array("f0","f1","f2","f3");
var secondArray = new Array("s0","s1","s2","s3");
for(var x = 0; x <= arrayIndex.length;x++) {
for(var i = 0; i <= 3;i++) {
Debug.Log((arrayIndex[x]).[i]); //★ this doesn't work the way I need
}
}
Can anyone point me out the way to make (★) work? And is this the right way of doing it, or should I just use a different dimension Array for this? If you could give me an example, that would be great. Thanks in advance.
What exactly is happening with this? I can't tell what you're trying to accomplish.
(arrayIndex[x]).[i] doesn't make a lot of sense. It's like you're trying to reference two different indexes on the same array.
If you could explain a bit why you need the 'arrayIndex' that could help. It seems like it would be easier without it.
sounds like you need a multi-dimensional array, for example :
var firstArray : String[] = ["f0","f1","f2","f3"];
var secondArray : String[] = ["s0","s1","s2","s3"];
var multiStringArray : String[,] = new String[ 2, 4 ];
for ( var x : int = 0; x < 2; x ++ )
{
for ( var y : int = 0; y < 4; y ++ )
{
switch ( x )
{
case 0 :
multiStringArray[ x, y ] = firstArray[y];
break;
case 1 :
multiStringArray[ x, y ] = secondArray[y];
break;
}
}
}
just off the top of my head, syntax may not be correct
Edit : I really should be asleep so the above code is terrible. You could also create a list of lists (I personally never use array, only built-in arrays or List)
Thanks alucardj, just what I needed. I'm sorry to those who didn't understand what I meant, but thanks for response!
Answer by FarbrorMartin · May 16, 2014 at 07:52 PM
I think I understand what you are trying to do. It would be easier to use a javascript object to hold your two arrays. Like this:
var arrays =
{
"first" : [ "f0", "f1", etc... ],
"second" : [ "s0", "s1", etc... ]
}
Then you can access your arrays like this:
var valueFromFirst = arrays.first[2];
var valueFromSecond = arrays.second[0];