Set Variable by Name

16 April 2017: custom function updated to facilitate auto-interpretation of dates (SetVarByName-v2.zip). See below for more info.

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…

2016-04-06_074440

…and then proposed an unwieldy mass of gobbledygook as a workaround, e.g.,

2016-04-06_083324

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

Definition

2016-04-06_142711

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

2016-04-06_104124

Result:

2016-04-06_103747

Runtime Code Example

2016-04-06_092852

 Result:

2016-04-06_092309

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…

2016-04-06_095832

…yields this:

2016-04-06_101353

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.


Post-script 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”.

2017-04-16_181311

4 thoughts on “Set Variable by Name

  1. Kevin Frank Post author

    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.

    Reply
  2. nicklightbody

    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

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s