PDA

View Full Version : eval() and referenced arrays


HapG
12-05-2006, 06:34 PM
I'm trying to reference and array being passed to another function. However, I can't structure the eval statement so that the array is referenced.

Below are a couple sample functions that show what i'm trying to do.
If i declare $test as a global string array, this will work, but I'd rather not do that.
If I leave it like this, i get an error:
// Error: ssZeroFunc($test); //
// Error: Line 1.17: "$test" is an undeclared variable. //

Any Suggestions?

global proc ssZeroFunc(string $test[]){

for($i=0;$i<size($test);$i++){
$test[$i] = "zero";
}

}


global proc ssEvalTest(){
string $test[] = {"one","two","three"};
string $func = "ssZeroFunc"; //this is dynamic, thus the need for the eval

eval($func + "($test)");
print $test; //should print all "zero"

}

thanks

instruct9r
12-05-2006, 07:11 PM
global proc string[] ssZeroFunc(string $test[])
{
for($i=0;$i<size($test);$i++)
{
$test[$i] = "zero";
}
return $test;

}

global proc ssEvalTest()
{
string $test[] = {"one","two","three"};
string $func[] = ssZeroFunc($test);

appendStringArray ($func, $test, 3);
print $func;}


this will return "zero" 6 times because you send ({"one","two","three"}) and then you reassign "zero" to $test...
1. You need to use return in the procedures if you want to get values that has been computed inside....
2. you cannot say this "$func + $test" because that is array.. instead you can use.. "appendStringArray" check in the help..

If you want to be printed: zero, zero, zero, one, two, three... you shoud do this...

global proc string[] ssZeroFunc(string $test[])
{
for($i=0;$i<size($test);$i++)
{
$test[$i] = "zero";
}
return $test;
}
global proc ssEvalTest()
{
string $test[] = {"one","two","three"};
string $func[] = ssZeroFunc($test);
string $test1[] = {"one","two","three"};

appendStringArray ($func, $test1, 3);
print $func;
}


or something like that.. because when you declare the $test[] you put inside one, two, three, but in the next line it returns $test from the other procedure and it's with different values and $func get that values too.. that's why in the first example you get printed 6 times "zero"

heh cheers

grantimus
12-05-2006, 07:58 PM
If you want to use a string array in an eval statement you have to convert that array into a regular string first. The string in your eval statement would have to read something like this eval("ssZeroFunc({\"one\",\"two\",\"three\"})"). Here is an example of how to do that dynamically:

eval("ssZeroFunc({\"" + stringArrayToString($test,"\",\"") + "\"})");

btw, don't let the Rushtons work ya too hard.

grantimus
12-05-2006, 08:27 PM
I should probably clarify my last post a bit, when you use the eval command the string that eval is evaluating gets evaluated outside of the procedure that is calling eval. Basically this means that eval won't recognize any local variables, because eval can't reference those local variables when it is evaluating your string. This is why your script worked when you declared $test as a global variable.

HapG
12-05-2006, 10:17 PM
instruct9r-
unfortunately, i'm using the return statement to return something else.
also, the ssZeroFunc can't be a regular call, because the actual function i'm calling will change dynamically. That's why i'm using the eval. If it was static, I could use your method and I wouldn't have any issues.

I can expand my original code to show this
global proc ssEvalTest(){string $test[] = {"one","two","three"};
string $functions[] = {"myFunc1","myFunc2","myFunc3"};
for($func in $functions){
eval($func + "($test)");
print $test;
}
}

The idea is to take advantage of maya's pointer/referencing of array vars. Any changes ssZeroFunc makes to the $test array will be made to the $test array inside ssEvalTest because ssZeroFunc is actually referencing the $test array from ssEvalTest, not making it's own version.

That is why i can't expand the $test array in the eval statement as you suggest grantimus. ssZeroFunc will no longer be referencing the $test array.

If eval isn't going to work without using a global var, any suggestions on another way to construct this so that I can pass the string array info without using

grantimus
12-05-2006, 10:34 PM
global proc ssEvalTest(){string $test[] = {"one","two","three"};
string $functions[] = {"myFunc1","myFunc2","myFunc3"};
for($func in $functions){
eval($func + "({\"" + stringArrayToString($test,"\",\"") + "\"})");
print $test;
}
}

sunit
12-05-2006, 11:15 PM
global proc ssEvalTest(){string $test[] = {"one","two","three"};
string $functions[] = {"myFunc1","myFunc2","myFunc3"};
for($func in $functions){
eval($func + "({\"" + stringArrayToString($test,"\",\"") + "\"})");
print $test;
}
}

unfortunately, this won't pass along a reference of your array. in order to use eval and references, you'll need to use a global var. i tend to set aside a global array just for this type of use.


global proc ssEvalTest()
{
global string g_stringArray[];

string $test[3] = {"one","two","three"};
string $returnedArray[];
string $functions[3] = {"func1","func2","func3"};

for ($function in $functions)
{
$g_stringArray = $test;
$returnedArray = eval($func+" $g_stringArray");
$test = $g_stringArray;
}
}


where $returnedArray represents whatever other value you're expecting in return from your function.

-sunit

grantimus
12-06-2006, 02:30 AM
unfortunately, this won't pass along a reference of your array. in order to use eval and references, you'll need to use a global var. i tend to set aside a global array just for this type of use.

Yeah, I gotcha. I misunderstood what he was after exactly. But, you're right without a global variable I think this would be impossible.

CGTalk Moderation
12-06-2006, 02:30 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.