FileMaker Extra: Recursive Scripts

 <  Day Day Up  >  

Chapter 14, "Specialized Calculation Functions," 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 the Recursive Add script below:

 

 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, then 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 two 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 is that the parameter is empty, which, of course, was the condition on the very first trip through the script.

Although it's certainly possible to create 100 new records with a looping script, that would typically require the use of a global number field to act as a counter. The recursive script requires no such additional field, and indeed this is the main advantage that recursive scripts have over looping scripts.

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.

 <  Day Day Up  >  


QUE CORPORATION - Using Filemaker pro X
QUE CORPORATION - Using Filemaker pro X
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 494

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net