FileMaker 18 released a new function called While.
Anybody who has done any programming in the past will be familiar with the concept of this function. In fact, the While function goes all the way back to BASIC, if you are old enough to remember that.
The While function’s purpose is to perform a certain action repeatedly until certain conditions are met. In many ways it is very similar to the custom recursive function, which I always found a bit confusing. The While function is doing basically the same thing, but it separates it’s actions out into distinct categories within the function, thus making it easier to understand.
The four sections of the While function are:
1. Parameters
2. Condition
3. Logic
4. Result
Parameters – you have to declare the variables which you are using in the function
Condition – the condition which must exist for the function to continue to execute its Logic
Logic – what you actually want to do
Result – the final result value you wish to capture.
Here is a section of the documentation from FileMaker:
While
Repeats logic while the condition is true, then returns the result.
Format
While ( [ initialVariable ] ; condition ; [ logic ] ; result )
Parameters
initialVariable – variable definitions that will be available to use in the following parameters.
condition – a Boolean expression evaluated before each loop iteration. While True, the loop repeats. When False, the loop stops.
logic – variable definitions that are evaluated each time the loop is repeated.
result – an expression that is returned when the loop stops.
Data type returned
text, number, date, time, timestamp, container
Originated in
FileMaker Pro 18.0 Advanced
Description
The While function performs the following steps:
1.Evaluates the initialVariable parameter.
2.Evaluates the condition parameter.
3.If condition is:
•True (any non-zero numeric result), evaluates logic, then repeats step 2.
•False (0), stops the loop and returns result.
You can specify multiple variable definitions for initialVariable and logic by enclosing them within brackets [ ] and separating each with a semicolon. For example:
While (
[ initVar1 = value1 ; initVar2 = value2 ; …] ;
condition ;
[ logicVar1 = expression1 ; logicVar2 = expression2 ; …] ;
result
)
The variables specified in the initialVariable and logic parameters are evaluated from left to right. These variables are within the same scope and can be used in the condition and result parameters. See Using variables.
Variables that need to retain information from one loop iteration to the next must be initialized before they are used in the logic parameter. Otherwise, their values are deleted.
Here is an example of the While function which I recently used:
While (
[ ~f = $foundcount ; ~i = 0 ; ~uniques = “” ];
~i < $foundcount + 1 ;
[ ~uniques = ~uniques & $id.client[~i] & “¶” ;
~i = ~i + 1 ];
~uniques )
I wanted to gather a list of unique client ID’s into a variable. (I took some additional steps to make them unique, but this was just the first part where I was gathering them together into one variable.)
Note that I had to declare variables, so I shortened them using the tilde (~) in front to identify them. (I use the tilde inside of LET statements also, as per Matt Petrowsky.)
Then the condition is that as long as ~i is less than the Found Count + 1, the Logic should continue to execute.
And what did I want to do? I wanted to gather all of the ID’s which had been assembled into a previous loop into the $id.client variable. (The “[” & “]” indicate the various iterations of that variable formed from a loop.) Also, the other bit of code to execute during logic (note that you can do more than one action, if you gather them in square brackets “[ ]”), is that ~i would be incremented by one each time. Once it reached the ~foundCount value , the function would stop.
The final result was ~uniques (not unique yet, but they will be), a gathering of all the ID keys into one variable, done in a single step, and instantly! It was very nice.
So the While function is an eminently useful function added to the vast toolkit of functions available to FileMaker developers.
[wpforms id=”7435″]