View Full Version : Get value from string of variable name

01 January 2011, 05:50 PM
How can I get value from string of a variable name. For example:

string $foo="$miImfkeyOverrideScriptJobNum";
print $miImfkeyOverrideScriptJobNum; print "\n";//i want this value

eval("print $foo"); print "\n";//does not work
print `eval("print $foo")`; print "\n";//does not work

Robert Bateman
01 January 2011, 05:59 PM
eval("print " + $foo);

01 January 2011, 06:01 PM
thats perfect...thanks

01 January 2011, 06:51 PM
Is there a way to do this without having to print the value but instead store it as a variable:
Something like this, if it worked.

string $foo="$miImfkeyOverrideScriptJobNum";
print $miImfkeyOverrideScriptJobNum; print "\n";//i want this value


01 January 2011, 07:23 PM
this is similar to how you cast global vars to python from mel...
$result = eval("$nul = " + $foo);(of course you can't use $foo again as the type is different) though I have no idea why you would ever need to do something like this. I smell something bad! ;)

01 January 2011, 04:23 AM
Thanks again. I couldn't really get what I want out of mel so I got it working with pymel.
Closet I got in mel:

string $envs[]=`env`; for($i=0;$i<=size($envs);$i++){
$size= size($envs[$i]);
print ( $i + "- " + $envs[$i] + " - " + $type + ": \n");
eval("print "+$envs[$i]);
print "\n";


for index, eachEnv in enumerate(MelGlobals().keys()):
print "%s- %-50s %s" %(index, eachEnv, melGlobals[eachEnv])

CGTalk Moderation
01 January 2011, 04:23 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.