Jump to content

Evaluating local variables


David Jondreau
 Share

This topic is 4344 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I'd like to pass a parameter to a custom function and return the local value for that parameter. I know that's a clunky way of saying what I mean. I want to code some values in a custom function return those values based on the parameter passed.

I want Eval ( "dog" ) to return "bark".

I tried this custom function:

Eval ( parameter )

Let( [

dog = "bark" ;

cat = "meow" ;

mouse = "squeak"

];

Evaluate ( parameter )

)

But that didn't work. I could use a Case () function but I expecting a lot of values in my list.

Thanks for any tips.

Link to comment
Share on other sites

Yes, you're right, it wouldn't be shorter. In fact, it the whole calc would evaluate faster, as not all the local variables would have to be evaluated. Once a true test is met, the Case() stops.

But now I realize there's something about Evaluate() I don't understand and I'd like to to learn why it doesn't work the way I think it should.

Link to comment
Share on other sites

But now I realize there's something about Evaluate() I don't understand

I believe Evaluate() does not "look back" at the parameters defined by Let() or by a CF. So for example in:

Let ([

code = "message" 

] ;

Evaluate ( "code" )

)



the formula to be evaluated is code - and this produces Error#102 (Field is missing), because code is perceived as meaningless. OTOH, 




Let ([

$code = "message" 

] ;

Evaluate ( "$code" )

)

will return "message" as expected.

Link to comment
Share on other sites

Or you could evaluate the whole thing:

Evaluate ( "Let ( [" & 

"dog = \"bark\" // etc.

& "] ;" & parameter & " )" )

Link to comment
Share on other sites

I use a CF that is a modified version of that to parse name/value pairs when I pass multiple script parameters. What's your preferred method to pass multiple script parameters?

Link to comment
Share on other sites

Yeah, OK - if they cannot be return-separated (my preferred method). Otherwise:

http://fmforums.com/forum/index.php?showtopic=39249&view=findpost&p=183000

However, with Set Variable[] I find the need for multi-valued parameter very rare.

Edited by comment
Link to comment
Share on other sites

I've been burned too many times by return-separated variables:

1. you have to be careful about the order

2. unreliable for passing lists

I find the need for multi-value parameters very common. I often find myself coming back to a button that originally passed a single param and redefining it to pass name/value pairs.

What I like about the custom function method is that it takes so little thought when defining the parameter, e.g.:

"name=" & nameField &

"; id=" & IDfield &

"; ice cream=good" // etc

And then in my script:

Set Variable( $param ; Get( ScriptParameter ))

GetNamed( $param ; "id" ) // GetNamed being the name of the CF

Link to comment
Share on other sites

I recently started using SetProperty( name ; value ) and GetScriptProperty ( name ) for passing parameters between scripts and I'm really like it: http://www.goya.com.au/blog/using-name-value-pairs-parameters-and-storing-data

I don't think that relates to my question though.

The Evaluate() won't take a Let-defined variable as a valid expression is what tripped me up. It seems the answer is simply, "The computer says no."

Link to comment
Share on other sites

Sorry if I derailed your thread, DJ. Michael asked why I would use Evaluate that way. (I still don't know why you're trying to use it the way you are.) B)

For any interested lurkers, I've posted the CF I use here:

http://fmforums.com/forum/topic/77148-passing-multiple-script-parameters/

Link to comment
Share on other sites

This topic is 4344 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.