combining two words into a variable?

Is there any way to combine two words into a variable, like for instance: the word “player” and a number to access the data stored in a variable called player1 or player5?

… or combining the words “snow” and “World” to get to the data in the variable snowWorld? Like some kind of concatenation, but with the variable names and not the contents of variables. (Preferably in javascript)

Oh, and I’m not allowed use an array… so forming an array called player and just calling the number using the index won’t fly :wink:

I hate repeating code unless I really really have to, so… help!

Thanks!

How about using hash table, since the key is a string, so that, you could combine any text you like :slight_smile:

http://www.go-mono.com/docs/index.aspx?tlink=37%40ecma%3A195%23Hashtable%2FM

apple_motion, I had the same thought. (And I love the Heinlein quote in your signature. I have it on my desk: “Progress isn’t made by early risers. It’s made by lazy men trying to find easier ways to do something.” Fantastic!)

Anyway, back to this thread. Albrektson, here’s some sample Javascript code using hash tables.

var h_info : Hashtable; //declare the type

function Start() {
    h_info = {}; //define the variable
    h_info["PLAYER"] = {}; //set up the second level of the hash to permit another hash level
    h_info["ENEMY"] = {}; //set up the second level of the hash to permit another hash level
}

function someScript() {
    h_info["PLAYER"][1] = "Brett"; //assign a value
    h_info["PLAYER"][2] = "Laura"; //assign a value
    h_info["ENEMY"][1] = go_some_game_object; //assign a value
    h_info["ENEMY"][2] = go_some_other_game_object; //assign a value
}

My company has set up a lot of variables that way. I hear it takes a lot of memory, but it addresses your original question.

Yeah, we are lazy !

Maybe the type casting cause the problem. In the past, (because of lazy) I offen simply append the hashtable value onto another string when I know the value is type of string.

h["key"] = "lazy";
var myself:String = "i am " +h["key"];

It is works and no error, but the speed is super slow. (may cause more memory too). Also, ToString() doesn’t work in this case too.

I found that, if I put it on a temp variable with static type first then the problem gone :slight_smile:

h["key"] = "not lazy"; 
var temp:String = h["key"];
var myself:String = "i am " +temp;