Jump to content

Passing multiple script parameters


Fitch
 Share

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

Recommended Posts

Posting this with permission from SeedCode. I have seen similar methods that require an additional function to pass the parameters in, but it doesn't seem to me to be an improvement. I've also seen methods that require more complex structuring of the parameter. What I like about this method is that once you create the function, it is so simple to use.

I call a script, and for the script parameter, pass in as many name/value pairs as required, e.g.

"id=" & IDfield &

"; name=" & nameField &

"; type=widget" // etc.

At the top of the script, I generally start with:

Set Variable( $param ; Get( ScriptParameter ))

... and then parse out the parameter values using the function below, which I call GetNamed:

Set Variable( $id ; GetNamed( "id" ))

Set Variable( $type ; GetNamed( "type" ))

(FWIW I've noticed in recent SeedCode files that they've replaced this CF with a script that does essentially the same thing.)

/* SeedCode_GetValue

Created by John Sindelar, SeedCode LLC

www.seedcode.com 

Creation Date: 13 Feb 2005

Last Modified Date: 21 Feb 2007  

 

Purpose:   

Extracts a value from a set of Name/Value pairs in a text string (such as a variable or field).

Where "Name" is the name in a Name/Value pair like: "Operation = Begin". 

Separate Name/Value pairs with semicolons like: "Operation = Begin ; Status = Estimate". 

Quotes are optional in the value: "Operation = Begin ; Response = \"OK\"" 

Fields can be entered like this: "Operation = Begin ; Status = " & job::status 

If a requested value is not present, the result is blank. 

 

Parameters:   

Text: the string within which you're looking for your value. Normally this is your variable or global field.  

Name: the name in the name / value pair you're looking for. 

 

Example: 

Given  this string in a variable called $Var: "Operation = Begin ; Status = Estimate". 

The following will return "Begin":

SeedCode_GetValue ( $Var ; "Operation" )



Requires Other Custom Functions:

None



*/ 



Let ( [ 

string = Substitute ( Text ; [ "\"" ; "^^" ] ; [ " ;" ; ";" ] ; [ ";" ; "\" ;" ] ; [ "= " ; "=" ] ; ["=" ; " = \"" ] ; ["¶" ; "~~" ] ) & "\"" ; 

eval = Evaluate ( "Let ( [" &  string  & "] ;" & Name & " )" ) ; 

result = Substitute (  eval ; [ "^^" ; "\"" ] ; [ "~~" ; "¶" ]  ) 

] ; 



If ( result ="?" ; "" ; result ) 



)



// End

Link to comment
Share on other sites

  • 1 year later...

I've been trying this process out and it seems to work well for short variables. However, for very long variables (in my case the contents of a 2000 line batch file), performance is very slow, taking a good few minutes for the variable passed to be made available to the script.

For now I'll put the file contents into a field earlier in the process, but if anyone knows of an efficient way of passing long variables between scripts without having to go via a field, I'd be interested to hear it.

Alec

Link to comment
Share on other sites

You may want to use a simple list of parameters rather than this function. Certainly the stripping/restoring of carriage returns is a performance hit when you have thousands of them. When including a multiple-paragraph variable in a list, you'll need to enclose it in a Quote function.

Alternately, if the variables are being used within a single file, you might just want to put them one at a time into global variables. For multiple files, you might have a script in the target file that handles that.

Link to comment
Share on other sites

  • 3 years later...

UPDATE: we still use this custom function for passing multiple parameters. Recently we had to modify it because we're doing some work with HL7 (a medical coding system) and it uses the '^' and '~' characters, which are used in this CF as placeholders for quote and paragraph characters. Note that HL7 also uses pipes, which I know some folks like to use as "safe" delimiters -- not safe here!

As a quick fix we opted to use a more obscure placeholder. We opted for a couple of currency symbols, ££ and ¥¥. You'd theoretically never see two of those in a row, and we never work with foreign currency so this is satisfactory for us. Obv. you can use your imagination to choose what is appropriate for your solutions.

Link to comment
Share on other sites

This topic is 2615 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.