28 Dec 2020: for further thoughts on this subject see Set Variable By Name Revisited.
29 July 2017: custom function updated to simplify the clearing of existing vars.
16 April 2017: custom function has been updated to auto-interpret dates (rather than treating them as sequential division problems)… e.g., to recognize “4/16/2017” as a date, as opposed to “4 ÷16 ÷ 2017”.
Recently, in the midst of various reporting and charting projects, I’ve found myself wishing for an easy way to “dynamically instantiate” one or more variables… in other words, set them by name, with the name determined programmatically, as opposed to being hard coded.
This issue has come up from time to time on this blog over the last five years or so, most notably in Dynamic Variable Instantiation, where I observed that…
…and then proposed an unwieldy mass of gobbledygook as a workaround, e.g.,
Actually, the preceding isn’t too bad for occasional use, especially if the value you’re passing to the variable is as simple as the GetSummary string shown above.
But as I began using this methodology on a more regular basis, and as the values I wanted to pass to the variable grew in complexity, it eventually dawned on me that a custom function would make my life easier… and not finding anything out there that exactly fit my needs, I located a viable candidate on Brian Dunning’s site, and proceeded to modify it.
I decided to call it SetVarByName, and it’s installed in this bare-bones file if you’d like to take it for a test spin: SetVarByName.zip
My goals for the CF
- simple to implement and use
- standalone — not part of a framework
- allow zero and negative repetitions (in addition, of course, to positive reps)
- allow $ and $$ variables to be defined (default to $ if unspecified)
- “value” automatically interpreted as either a static text string or as runtime code
- provide an easy way to clear a variable
Definition
One of Vaughan Bromfield’s improvements to the original CF was to wrap the main portion in EvaluationError — which returns a zero when there are no obvious problems with your syntax. I took the idea a step further and added a separate EvaluationError test to the initial Let declaration, facilitating the CF’s ability to guess whether you’re passing it a static text string or runtime code that needs to be interpreted.
Static Value Example
Result:
Runtime Code Example
Explanation:
- $counter = 2
- “$col_” & $counter = “$col_2”
- $col_2 = 12
- $col_1 = 3
- $col_2 / $col_1 = 4
Overriding Automatic Detection of Runtime Code
If, for some odd reason, you need to override the automatic detection of run-time code, then use a combination of Quote and, if necessary, escaped quotation marks, e.g., this…
…yields this:
The point being, with this CF, you save the syntactical contortions for once-in-a-blue-moon scenarios, whereas most of the time it does what you want with a minimum of fuss.
At any rate, it’s making my life easier, and we’ll see it in action next time around when we begin a multi-part series on Virtual List Reporting.
Finally, I want to say thank you to Rob Russell, Howard Schlossberg, Geoff Gerhard and Darren Terry, who provided useful feedback and suggestions regarding the custom function.
Demo file has been updated to implement the accidentally-unimplemented “auto-prepend unspecified $ to var name” feature. Demo includes a test script to verify feature works.
Hey Kevin
Here is what I published in 2014 on this subject – building on your earlier post – as part of my multi language support.
Thanks for your latest piece,
Cheers Nick
Designing for language:
Nick Lightbody
7th October 2014
https://community.filemaker.com/thread/78347
“The trickiest part for me was finding out how to auto publish the global variable – here is my method – although I did this a couple of years ago I think I recall getting the key bones of this from Kevin Frank’s excellent blog – http://www.filemakerhacks.com – sorry not to be clearer on that attribution – it was a little while ago.
Let ([
variableName = langData::a1PublishTo ; // the global variable name specified in the language table
variableValue = langData::a1RecordTitle[$$Lang] ; // the language string to be used from the repetition for the selected language
formula = “Let ( variableName = variableValue ; “” )”
];
Evaluate (
Substitute (
formula ;
[ “variableName” ; variableName ] ;
[ “variableValue” ; Quote(variableValue) ]
) //close Substitute
) // close Evaluate
) //close Let
“
Kevin,
Thank you always for sharing useful information.
Here you can find a Japanese translation of the article:
http://notonlyfilemaker.com/2017/03/set-variable-by-name/
Custom function updated to facilitate easy clearing of variables like so:
SetVarByName ( $someVarName ; 1 ; "" )
This is very cool.Thanks!
I have an issue though with phone numbers. For example, if a field has a phone number formatted with dashes: 800-555-1212, the function sees it as a calculation and returns -967
Hi Todd,
To work around the CF “helpfully” interpreting the phone number as a subtraction problem, do this instead:
SetVarByName ( "$whatever" ; 1 ; Quote ( "800-555-1212" ) )
Hope this helps,
Kevin
Thanks! I’ll try that. Another issue that appears to be a FileMaker issue not a CF issue is that if a field contains the text OTHER, Evaluate (and therefore the CF) will return the number 10. I’ll see if quoting that field will fix that as well.
FileMaker has a small number of “reserved words”, as documented by Ray Cologon here —
http://nightwing.com.au/FileMaker/Resources/ReservedWords.html
I assumed that was this case. Thanks for verifying!