The Support Group Blog

Dynamically naming a variable in FileMaker Pro

Written by The Support Group | Mar 23, 2010 3:02:07 PM

One of the things I find I need to do frequently is create a variable using a calculated name.  For example, I can use the FieldNames function to get the list of fields on a layout. I might want to then declare a number of variables that have the same names as the fields, and then populate them with those fields’ values.  Later, I can loop through the fields again, perhaps updating them with a Set Field By Name step, and reference the variable with the similar name.

Or, I might want to create a number of nearly-identically named variables, such as $$day1$$day2$$day3, and so on, up to $$day99.  Instead of using ninety-nine Set Variable steps in a script, it should be possible to create a loop: calculate the variable name, declare it with Set Variable, and repeat ninety-nine times.  Unfortunately, while the Set Variable step allows us to calculate the value of a variable, and the repetition to be set, it does not allow us to use a calculation to determine the name. What we can do instead, though, is use the Let function to set a variable.  Combined with the Evaluate function, we can dynamically name our variables.

As an aside, in our $$dayXX example, we COULD use repetitions of one variable instead of separate variables.  Our loop would increment a counter ($n) by 1 each time, set the $nth repetition of $$day, and exit the loop when $n hit 99.  We’d end up with $$day[1] through $$day[99].  This might serve most purposes, but it won’t allow us to use FileMaker Pro 11’s ability to display variables as merge text or use as part of stored find criteria—these features can’t reference anything but the first repetition.

Let’s say you have the following formula:

Let ( $$day1 = "1/1/2010" ; "" )

This doesn’t return anything ( the calculation part of the Let formula is the empty string ”“), but it creates the global variable $$day1 with the value 1/1/2010, which can then be used throughout the file until emptied (or the file is closed).  Using the $ or $$ symbols as part of the variable declaration in a Let function creates local and global variables the same as a Set Field step.

To give the variable a dynamic name, we first calculate—as a text string—what the formula would be to create that variable, and then Evaluate that string.  If we take our original formula, properly escape the quotation marks with backslashes, and turn it into a text string by wrapping it in quotes, we can Evaluate it and get the same resulting global variable.

Evaluate ( "Let ( $$day1 = "1/1/2010" ; "" )" )

The basic formula is this:

Evaluate ( "Let ( variableName = Quote(variableValue) ; "" )" )

We can then use the Substitute function to produce a formula:

Let (
[
variableName = "$$day1" ;
variableValue = "1/1/2010" ;
formula = "Let ( variableName = variableValue ; \"\" )"
] ;

Evaluate (
Substitute (
formula ;
[ "variableName" ; variableName ] ;
[ "variableValue" ; Quote(variableValue) ]

) //close Substitute

) // close Evaluate

) //close Let

If we set variableName and variableValue to more dynamic values, such as other variables in a script, and then finally trigger evaluating the formula by setting some other unneeded local variable, we finally get our dynamically named variables.

Set Variable [$n; Value:0]
Loop
Set Variable [$n; Value:$n+1]
Exit Loop If [$ > 99]
Set Variable [$varName; Value:"$$day" & $n]
Set Variable [$varValue: Value:Date ( 1 ; $n ; 2010)]
Set Variable [$whatever; Value: //see below ]
End Loop

The formula for the last Set Variable step is:

Let (
[
variableName = $varName ;
variableValue = $varValue ;
formula = "Let ( variableName = variableValue ; \"\" )"
] ;

Evaluate (
Substitute (
formula ;
[ "variableName" ; variableName ] ;
[ "variableValue" ; Quote(variableValue) ]
) //close Substitute
) // close Evaluate

) //close Let

Of course, you do need to take care with this example.  I’m not testing to make sure $varName is in fact a valid variable name.  And for the majority of the time when you can use a hardcoded variable name, it doesn’t make much sense to use this technique.  But when you do need it, this technique will allow you to dynamically name your variables.

Download Sample File