First of all, props go out to Jeremy Bante for this one.
I’ve spent some time recently going through some of the solutions on ModularFileMaker.org. While I’ve found a lot of good stuff on there, most of it is very focused, and you get exactly what you’d expect out of them. I went through one of Jeremy’s modules, and not only did I get what I expected out of it, but found this hidden gem in there too.
I’m developing a solution for a client, and within one of my scripts, I need to set up a loop structure that will run a set number of times. Since FileMaker Pro doesn’t have a native “For Loop” like a lot of programming languages do, we’re left to our own devices to make sure that our Loop will repeat the desired number of times. This typically involves creating variables like $iteration, $maxLoops etc…
Jeremy’s approach is still doing this, but handles it in a much more elegant way (IMHO). Instead of using separate script steps for “Set Variable [ $iteration , ... ]” and “Set Variable [ $maxLoops , ... ]“, he’s moved these variable declarations into a Let statement right within the Exit Loop step. This keeps things looking very tidy. I’ve made a slight modification to what he did, simply by moving his code into a custom function. I find it makes things that little bit more readable. For those of you using FileMaker Pro Advanced, you’ll be able to do the same.
Here’s what it looks like:
Create a new custom function called “LoopNumberIsGreaterThan__cf”
It will have only one parameter –> I named it “N”.
The guts of the custom function are as follows:
/* For $i from 1 to N */
Let ( [ $i = $i + 1 ] ;
$i > N
and Let ( $i = "" ; True ) // reset $i at end
Use that custom function in a “Exit Loop If” Script Step immediately after a Loop begins.
Please note that $i will be your loop counter
How It Works
The first time the custom function gets called, $i does not exist. FileMaker treats a variable that doesn’t yet exist, the same way it would for a variable that does exist with a NULL value. The custom function will try to add 1 to the ‘existing’ value of $i, and in so-doing will create the variable, and instantiate it at 1. This explains why we use this at the beginning of the loop. Now, for our entire first loop, we’ll be able to see that $i has a value of 1.
Subsequent loops will continue to iterate the value of $i until such a point where $i has a value greater than the number that was passed to our custom function. At this point in time, the clever use of the logical operator “AND” is invoked which will use FileMaker’s capability to short-circuit operations. Basically, if $i is not > N, then the second half of that statement will never be evaluated (because it will always return a 0 anyway… regardless of what the second half of the statement says). When $i > N however, the second half of that statement does get evaluated, and it will actually reset $i to NULL. Destroying the variable so that you will be able to use it again in the future.
I’ve always made a habit of cleaning up my variables when I no longer need them, but didn’t think a lot of FileMaker developers were doing this. A pleasant surprise to find this indeed.