Chapter 14, "Advanced Calculation Techniques," discusses how you could make custom functions recursive by including calls to themselves within their formulas. In a similar manner, you can use script parameters to create recursive scripts. Although this isn't something you need to do on a daily basis, there are some interesting applications for recursive scripts.
A recursive script is one that calls itself repeatedly until some exit condition is satisfied. Each time the script calls itself as a subscript, it passes a script parameter that can be used as part of an exit condition test. In many ways, recursive scripts are quite similar to looping scripts, and many of the tasks you can accomplish with one can be done as easily by the other.
As an example of a recursive script, consider this Recursive Add script:
If [Get (ScriptParameter) >= 100] Exit Script End If New Record/Request Perform Script ["Recursive Add"; Parameter: Get (ScriptParameter) + 1 ]
This script adds 100 new records to the current table. It's first called without a script parameter, so the first time through, the script calls itself as a subscript, passing a parameter of 1. The parameter increments each subsequent time through, until eventually the exit criteria (Get (ScriptParameter) >= 100) is met.
If there are any steps in the script after the recursive subscript call, these are all executed, from the inside, out, after the exit criteria has been met. Try to predict what would happen if you added the following steps to the end of the preceding script:
Beep Show Custom Dialog ["The parameter is:" ; Get (ScriptParameter)]
The 100 records would be created exactly as they were originally. But after they were all created, you'd hear a beep and see a message telling you that the script parameter value is 99. After clicking OK, you'd then hear another beep and a message telling you that the parameter is 98. This would continue for some time, and eventually the last message you'd see would be that the parameter is empty, which, of course, was the condition on the very first trip through the script.
As a final example of recursive scripting, consider the following script, which flags duplicates among a set of records. Assume that the set contains a list of names, which has been sorted by name before this script is called:
If [IsEmpty (Get (ScriptParameter))] Go to Record/Request/Page [First] Else Go to Record/Request/Page [Next; Exit after last] If [Get (ScriptParameter) = Contacts::Name] Set Field [Contacts::DuplicateFlag; "Duplicate"] End If End If Perform Script ["Mark duplicates"; Parameter: Contacts::Name]
During each iteration through the script, the current record's name is compared against the value of the script parameter, which was set to the value of the previous record's name. The exit condition here is the Exit after last option on the fourth line; the script continues through the set of records, stopping only when there's no next record to go to.
Part I: Getting Started with FileMaker 8
Using FileMaker Pro
Defining and Working with Fields
Working with Layouts
Part II: Developing Solutions with FileMaker
Relational Database Design
Working with Multiple Tables
Working with Relationships
Getting Started with Calculations
Getting Started with Scripting
Getting Started with Reporting
Part III: Developer Techniques
Developing for Multiuser Deployment
Advanced Interface Techniques
Advanced Calculation Techniques
Advanced Scripting Techniques
Advanced Portal Techniques
Debugging and Troubleshooting
Converting Systems from Previous Versions of FileMaker Pro
Part IV: Data Integration and Publishing
Importing Data into FileMaker Pro
Exporting Data from FileMaker
Instant Web Publishing
FileMaker and Web Services
Custom Web Publishing
Part V: Deploying a FileMaker Solution
Deploying and Extending FileMaker
FileMaker Server and Server Advanced
Documenting Your FileMaker Solutions