Game Development Community

Hashtables/Arrays like PHP.

by Eros Carvalho · in Torque Game Engine · 02/18/2005 (5:45 am) · 7 replies

Im having a problem trying to access an array with a string in a variable.

Lets explain better, I want an array indexed by strings, and want access them by strings like this:


$arr["str1"] = "something1";
echo($arr["str1"]); // outputs: something1

this I tested and works fine, but my problem is when my index string is in a var like this:

%var = "str1";
echo($arr[%var]); // this outputs nothing


how I can get this working? in php this works ;P .. but I dont know how to get this working on torque scripts...


thanks

#1
02/18/2005 (6:11 am)
Well your problem is that there isn't any "$arr[%var]" array - just a "%var" variable. Variables are never passed into arrays automaticly so you would have to define it as $arr[%var] which would be less efficiant, unless you need this to be carried around. To display %var you would have to 1) have it in a function and 2) do this:
function fooEcho()
{
   %var = "str1";
   echo(%var);
}

the main differance from yours is that the computer isn't looking for an array that doesn't exist. I'm not that helpfull am I?

Ishbuu
#2
02/18/2005 (6:30 am)
This wont solve my problem. :)
But I really need this to get things a lot better and dynamic on my project...

i tried everything... like:

eval( "%content = $arr[\"" @ %var @ "\"];"); // same as this: %content = $arr["str1"];
echo(%content); // outputs nothing too...

Im getting very crazy with this torque crap string manipulation .
#3
02/18/2005 (7:38 am)
Here is what you are doing, you are setting the value of the variable to "str1". Then you are trying to get the value of an array index named "str1", which since you havent given it a value is nothing. You need to give the array index a value like this:

%var = "str1";
$arr[%var] = "something1";
echo($arr[%var]);

Or even something like this if you wanted to assign the array index to some dynamic value:

%var = "str1";
%var2 = "something2";
$arr[%var] = %var2;
echo($arr[%var]);
#4
02/18/2005 (7:48 am)
@Bitcrafters - strangely, the variable indexed array works fine for me when running a script in the console.

script -

echo("String as index");
$arr["str1"] = "something1";
echo($arr["str1"]);

echo("var as index");
%var = "str1";
echo($arr[%var]); 

echo("var with space as index");
%var = "str1";
echo($arr["" @ %var]);


output -

==>exec("control/client/test.cs");
Loading compiled script control/client/test.cs.
String as index
something1
var as index
something1
var with space as index
something1
#5
02/18/2005 (7:59 am)
@David

Did you try doing those sections seperately?

The reason your "var as index" and "var with space as index" both output "something1" is because you have already assigned that value to $arr["str1"] in the "string as index" section of your code.

EDIT: After looking at it a bit more it seems we are explaining a couple different things. You are replying to his post about being able to use string manipulation to create the index name which works. I am replying to the flaw in his code that doesn't allow him to see a value when accessing the array index, which is he hasn't given it a value.
#6
02/18/2005 (8:16 am)
@ Todd - I was assuming he had already assigned the array["str1"] variable to "something1" and couldnt call it using array[%var] with %var="str1".

If the assumption is wrong then the output of "" would be correct for an unassigned string variable.

EDIT Without seeing the code in context, we may be left wondering. My only other thought was is %var still in scope when used as the array index??
#7
02/18/2005 (8:56 am)
Thanks everyone!
It was a confusion mine here, now everything is working fine.