David Jondreau Posted March 15, 2011 Posted March 15, 2011 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.
comment Posted March 15, 2011 Posted March 15, 2011 I could use a Case () function but I expecting a lot of values in my list. Is using Let() any shorter? IIUC, you still need to define each input/output pair.
David Jondreau Posted March 15, 2011 Author Posted March 15, 2011 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.
comment Posted March 15, 2011 Posted March 15, 2011 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.
Fitch Posted March 15, 2011 Posted March 15, 2011 Or you could evaluate the whole thing: Evaluate ( "Let ( [" & "dog = \"bark\" // etc. & "] ;" & parameter & " )" )
comment Posted March 15, 2011 Posted March 15, 2011 Or you could evaluate the whole thing: True, but then why would you?
Fitch Posted March 15, 2011 Posted March 15, 2011 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?
comment Posted March 15, 2011 Posted March 15, 2011 (edited) 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 March 15, 2011 by comment
Fitch Posted March 15, 2011 Posted March 15, 2011 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
David Jondreau Posted March 15, 2011 Author Posted March 15, 2011 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."
Fitch Posted March 15, 2011 Posted March 15, 2011 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.) For any interested lurkers, I've posted the CF I use here: http://fmforums.com/forum/topic/77148-passing-multiple-script-parameters/
Recommended Posts
This topic is 5000 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 accountSign in
Already have an account? Sign in here.
Sign In Now