- Home /
Possible to use an array value as the name of a variable?
I am trying to name a variable. I want to use the value of an array location as the name. The reason is that I am loading data at runtime and it is stored in an array. For example, the contents of myfile.txt are:
apple,dog,tree
I would like to name a variable to whatever information is in the first location of the array, and then another variable using the second location.
Is this possible?
Javascript only please. I can barely script with that, let alone learning another language.
Thanks in advance.
Answer by kennypu · Apr 14, 2011 at 12:36 AM
say your array is:
var myArr:String[] = ["apple","dog","tree"];
then to access each of those you do:
print(myArr[0]); //which will print apple
print(myArr[1]); //which will print dog
print(myArr[2]); //which will print tree
Basically, yes. How I get to the array is done differently, but it won't make any difference.
wait, what is your question? looking at it again, are you trying to 'name' a variable to the value of the array? No, you can't do that because technically, a name of a variable doesn't matter, its just a way for the computer to differentiate between 1 from another. If you want to set the value of a variable to the value in the array, you can do like var exampleVar = myArr[0].
This seems to be what I was afraid of. I just wanted to be sure.
Answer by Eric5h5 · Apr 14, 2011 at 02:02 AM
Are you talking about a hashtable?
import System.Collections.Generic;
function Start () { var things = new Dictionary.<String, int>(); things["apple"] = 0; things["dog"] = 1; things["tree"] = 2; Debug.Log (things["tree"]); }