Brian Dunning's FileMaker Custom Functions

ALoopingDemo ( AnyText )

This demo shows how to create a conditional loop within a custom function (similar to Do-Loop; Do-While and For-Next).

  Average rating: 4.7 (26 votes) Log in to vote

Doug Staubach   Doug Staubach
FileMaker Poweruser
https://www.linkedin.com/in/dougstaubach

Share on Facebook Share on Twitter

  Sample input:
ALoopingDemo("Test")
  Sample output:
Test.1.2.3.4.5.6.7.8.9.10

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

WHY THIS TECHNIQUE IS NEEDED:

1. FileMaker custom functions do not support looping statements (Do, While, For-Next) or line-jumping (GoTo).

2. FileMaker custom functions do not support the use of "optional parameters" (parameters that do not have to be included with the function call), which might help to simulate some of the above functions.

The code demonstrated in this function shows how to create a conditional loop using Case(), Let(), Recursion, and a local script variable ("$", not "$$").

Using this technique, you can create a loop with the ability to perform different actions at each "step" in the loop (1, 2, 3, etc.).

DEPENDENCIES:

1. Tested using FileMaker 13 - should work on v12 also.

2. Does not have any dependencies on any other custom functions.

3. Does not require a secondary function to keep track of counters.

NOTES:

1. I did not invent this technique. It has been in existence for a long time (if you know the original author, please let me know so I can give them credit). I am just showing a simple example for people who want to include some sort of looping mechanism inside of a FileMaker custom function, and haven't tried this method before.

2. FileMaker has a limit of 10,000 nested recursions per function call. Therefore, it makes sense to use this technique for an internal loop counter, but it is not good to use this technique for looping through large data sets (in that case, you might want to check out the looping capabilities of FileMaker scripts, which have fewer limits).

3. You will need a copy of "FileMaker Advanced" to *create* a custom function, but functions that have been created with "FileMaker Advanced" and saved into a database, can be freely used by "FileMaker Pro" clients who access that database.

If you like this function, please rate it (scroll up, under the function name), or leave a comment below.

Doug Staubach

 

Comments

unix   unix, Japan
Mar 22, 2015
Take care of the $variable name, since it is shared with script.

Set Variable [ $LoopCount ; 5 ]
Show Dialog [ ALoopingDemo("Test") ]
 
Brian   Brian, Mexico
Mar 25, 2015
Very well explained and exposed. congrats!
 
jLT StructuresFromSilence   jLT StructuresFromSilence, Kendv,IN USA
Mar 27, 2015
Boy, i use and studied many CF but this is one CF i cannot see ANYTHING from its example! Why and how got numbers 1-9?? sounds really neat though!!
 
Doug Staubach   Doug Staubach, Denver, Colorado, USA
Mar 27, 2015
Hi all:

Unix - yes, you can start the loop midway if you set the $LoopCount to 5 before you call the function.

Brian - thank you very much, glad you found it useful!

jLT - The numbers 1-9 come from the middle loop (AnyText = AnyText & "." & $LoopCount)

Best wishes,
Doug
 
Hans Erik Hazelhorst   Hans Erik Hazelhorst, Utrecht, Netherlands
Jun 27, 2015
Extremely useful function! Thank you very much for taking the time to explain.

This is a template function that should deserve a special place in the function list, because it is really the starting point for many other CF's.
 
Doug Staubach   Doug Staubach, Denver, CO USA
Nov 12, 2015
Hi Hans:

Thank you; I'm glad that you found it so useful!
 
Andreas Fischlin   Andreas Fischlin, Zurich, Switzerland
Feb 23, 2018
Above solution has a disadvantage, since it forces the core algorithm to be done in the loop to be defined twice. Following solution avoids that:

Case (
not $ix ; Let ( [ $OtherVariable = "" ; $ix = from ] ; MyLoop ( from ; to ; AnyText ) ) ;
$ix ≤ to ; Let ( [ <do something with AnyText> ; $ix = $ix + 1 ] ; MyLoop ( from ; to ; AnyText ) ) ;
$ix > to ; Let ( [ $OtherVariable = "" ; $ix = "" ] ; AnyText )
)

If you look at above FM script with a monofont, you can see better what is going on. A simple example not needing $OtherVariable (similar
to what is done above) would be:

Case (
not $ix ; Let ( [ $ix = from ] ; MyLoop ( from ; to ; AnyText ) ) ;
$ix ≤ to ; Let ( [ AnyText = AnyText & "." & $ix ; $ix = $ix + 1 ] ; MyLoop ( from ; to ; AnyText ) ) ;
$ix > to ; Let ( [ $ix = "" ] ; AnyText )
)

Again look at it with a monofont to understand better. Hope this helps.
 
Isaac Zhao   Isaac Zhao, Shanghai China
Jul 24, 2018
what a so wonderful CF ! thanks a lot
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Under construction. Email me your wish list for improvements.