drmerman
09-23-2006, 01:28 PM
Hey guys. I've come across a problem and I really can't figure it out at all. I was wondering if anybody has any ideas.
The following works :
//a word is entered into a textfield. Temporarily we shall assign it a constant value
string $dynamicWord = "officeBlock";
//after getting the word from the textfield, I want to use it to create a variable, in
//the form = $ <entered word> <attribute> = <value>. In order to create a variable using
//the above string as a name, we first have to create the command in a string.
string $variableCommand = ("string $" + $dynamicWord + "_fish = \"Red\";");
//if we print this we get : string $officeBlock_fish = "Red";
//we then evaluate $variableCommand to create the above variable
eval ($variableCommand);
//the variable, $officeBlock_fish, now exists, and has the value "Red"
The question is, how do I access this variable, bearing in mind it is dynamic?
Say that I want to get the value from $officeBlock_fish, and put it in another variable. I thought I would do the following, but it does not work.
//create a variable containing a short version of the 1st variables name
string $variableShortName = ("$" + $dynamicWord + "_fish");
//this gives us, $officeBlock_fish
//using the eval command, assign $officeBlock_fish to $newVariable
string $newVariable = `eval($variableShortName)`;
I keep on getting syntax errors no matter what I try.
I apologise if this is not explained very well, its been a long day and my brains been running around in circles :scream:
Any help would be really great.
Cheers,
Dr Merman
The following works :
//a word is entered into a textfield. Temporarily we shall assign it a constant value
string $dynamicWord = "officeBlock";
//after getting the word from the textfield, I want to use it to create a variable, in
//the form = $ <entered word> <attribute> = <value>. In order to create a variable using
//the above string as a name, we first have to create the command in a string.
string $variableCommand = ("string $" + $dynamicWord + "_fish = \"Red\";");
//if we print this we get : string $officeBlock_fish = "Red";
//we then evaluate $variableCommand to create the above variable
eval ($variableCommand);
//the variable, $officeBlock_fish, now exists, and has the value "Red"
The question is, how do I access this variable, bearing in mind it is dynamic?
Say that I want to get the value from $officeBlock_fish, and put it in another variable. I thought I would do the following, but it does not work.
//create a variable containing a short version of the 1st variables name
string $variableShortName = ("$" + $dynamicWord + "_fish");
//this gives us, $officeBlock_fish
//using the eval command, assign $officeBlock_fish to $newVariable
string $newVariable = `eval($variableShortName)`;
I keep on getting syntax errors no matter what I try.
I apologise if this is not explained very well, its been a long day and my brains been running around in circles :scream:
Any help would be really great.
Cheers,
Dr Merman
