Core FileMaker Blog

Keep up to date with CoreSolutions

“For Loops” in FileMaker – The Elegant Way

Texas_Loop_4.svg
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.

Typical use-case:
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:

Step 1:

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
		)
									

Step 2:

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

For Loops In Action

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.

Leave a comment

4 comments

  • Matthew Leering
    Matthew Leering 20 September 2013 Report

    Good points Jeremy.
    I had observed the fact that I would be locked-in to using a predetermined variable as my iterator, but don't often find myself having to nest my for loops.

    In most instances I can think of where I'd need to do that, I'm able to take advantage of a built-in FileMaker construct to do it instead (example:
    Loop/
    GoToRecord[ExitWhenLast]/
    EndLoop)

    Since it seems so infrequent that I need a nested for loop, and because moving this code into a custom function gives the benefits of both enhanced readability, and enforcing consistency of iterator names, I think I'm going to stick to this technique for the time being, and reevaluate next time I need a nested for loop.

    Thanks for the link though, I'm new to filemakerstandards.org, and hadn't seen that discussion on there yet.

    Comment Link

  • Matthew Leering
    Matthew Leering 20 September 2013 Report

    Thanks Daniel, and good to hear from you. I've got your Weetbicks blog in my RSS stream and always look forward to your updates.

    Comment Link

  • Daniel Wood
    Daniel Wood 20 September 2013 Report

    Love it !

    Comment Link

  • Jeremy Bante
    Jeremy Bante 20 September 2013 Report

    I'm not the first person to do this kind of loop iteration. FileMakerStandards.org has a page about it (http://filemakerstandards.org/display/bp/Iterators); I don't think Matt is the first person to do this, either.

    There are other custom functions for this, too; but I don't use them, even when I'm not trying to minimize dependencies for ModularFileMaker.org. Custom functions are less transparent about what their doing, which will slow down future maintenance developers who may not be as familiar with the functions I like to use as I am.

    One possible issue with your particular function is that the iterator variable is hard-coded in the function, so it wont work with nested For loops, and locks developers into your choice of iterator variable. (I'm less concerned about the latter issue; if a developer is adopting your functions, there's a good chance they adopt your conventions, too.) Other functions exist that deal with this by accepting a parameter for the iterator variable name, but this means they use Evaluate ( Let ( ... ) ) to set the variable, which is relatively slow.

    I use a Text Expander snippet instead. Recent versions of Text Expander include a feature for 'fill-in' snippets, so when I trigger that snippet, I only have to type the iterator variable name and the max value once, and the snippet repeats those where they need to be in the rest of the calculation. It also means I don't have to remember to install a custom function before using the technique in a new file (with the caveat that I have to be on my own computer.)

    I actually resisted the technique when I first saw it. I figured that putting all the functionality in a single calculation obscures what it's doing more than splitting it out into separate script steps. I'm still on the fence about that. Using a For loop template script snippet in Clip Manager would be nearly as fast as the Text Expander snippet.

    Comment Link

CoreSolutions Logo

London Office:
1-1615 N Routledge Park
London, ON N6H 5L6

Toronto Office:
2425 Matheson Blvd East
7th Floor
Mississauga, ON L4W 5K4

Toll Free: (800) 650-8882
London: (519) 641-7727
Toronto: (416) 410-8649
Email: This email address is being protected from spambots. You need JavaScript enabled to view it.

SUBSCRIBE

Click the 'Subscribe' button to sign up for news & software tips.

Beyond Software

Copyright © 2018 CoreSolutions Software, Inc. All Rights Reserved