Brian Dunning's FileMaker Custom Functions

CreateVariables ( text )

Create variables from a text string like "Name=Jai;Age=1"

  Average rating: 4.1 (32 votes) Log in to vote

Chris Bartlett   Chris Bartlett - Show more from this author

Share on Facebook Share on Twitter

  Sample input:
CreateVariables ( Get ( ScriptParameter ) )

For this example the script parameter is "Name=Jai;Age=1"
  Sample output:
$Name="Jai";$Age="1"

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

This custom function generates local variables from a text string without any prior knowledge of what those variables might be.

Passing named parameters into a script is a useful way of passing in more than one value. Other methods of doing this require the named parameters to be parsed out one by one or they use global variables that should then be cleared at the end of a script. This method requires a single script step and simply-formatted input string (such as a script parameter).

An input string needs to be in name=value pairs, delimited by semi-colons; e.g., "Name=Jai;Age=1"
(Note: not "Name=\"Jai\";Age=\"1\"")

At the top of a script, have a script step that uses the custom function to create the variables:
Set Variable [$Parameters ; Value: CreateVariables ( Get ( ScriptParameter ) ) ]
The variable $Parameters is just a dummy and serves no purpose other than to call the custom function.

The custom function does two key things:
1. It adds the required formatting to the string - e.g., "Name=Jai;Age=1" becomes "$Name=\"Jai\";$Age=\"1\""
2. It uses Evaluate() and Let() to actually create the variables $Name and $Age and give them the specified values.

 

Comments

Kevin Frank   Kevin Frank, Arcata, California
Apr 16, 2011
This is a great CF to parse named subparameters. I extended the substitutions to allow single spaces around the "=" and the ";" -- including inadvertently imbalanced ones, such as " ="...

[ " = " ; "=" ] ; [ "= " ; "=" ] ; [ " =" ; "=" ] ; [ "=" ; "=\"" ] ; [ " ; " ; ";" ] ; [ " ;" ; ";" ] ; [ "; " ; ";" ] ; [ ";" ; "\";$" ]

FWIW,
Kevin
 
Johan Sölve   Johan Sölve, Sweden
Aug 12, 2014
If there are quotes in the input text, they need to be escaped.
Add this substitution to take care of this.
[ "\"" ; "\\\"" ]
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 20 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: