PDA

View Full Version : Get value from string of variable name


fallenrelics
01-21-2011, 04: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-21-2011, 04:59 PM
eval("print " + $foo);

fallenrelics
01-21-2011, 05:01 PM
thats perfect...thanks

fallenrelics
01-21-2011, 05: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

$foo=eval($foo);

NaughtyNathan
01-21-2011, 06: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! ;)
:nathaN

fallenrelics
01-22-2011, 03: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++){
$type=`whatIs($envs[$i])`;
$size= size($envs[$i]);
print ( $i + "- " + $envs[$i] + " - " + $type + ": \n");
if($size>0){
eval("print "+$envs[$i]);
print "\n";
}
}

Pymel:

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

CGTalk Moderation
01-22-2011, 03: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.