Jump to content
Server Maintenance This Week. ×

Can Scripts Extract Data rather than insert it?


This topic is 609 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hi, this is not exactly my application but mimics the structure of what I am trying to do.  Imagine a chessboard, with each record in a squares table specifying in separate fields the row, column and chess piece (if any) filling it.  In each record (for each square) I also want to list all the chess pieces that can reach that square in one move.  

To do that, for each square X, I want to look at each other square, see what piece is in it if any, and then calculate whether the piece can reach square X, and then enter data in square X's record indicating that that kind of piece can reach square X.  That kind of thing would be pretty easy to do in C using a for-loop, but I'm not sure how to approach it in Filemaker.

Is it possible to run a script that loops through every square other than X, reads off the piece in that square if any, does a calculation to see whether the piece can reach square X and then makes a change in square X's record indicating that X is reachable by the piece, exiting the loop after every square has been examined?  To do the calculation, one would need to extract information about the row and column of each square and what kind of piece is in it. But I don't see how to extract that information.  The script functions I see only allow insertion of information into a field not extraction of information from the field.

I am not just interested in whether scripts are the best way to perform this job, but also whether scripts can do this kind of thing at all.

Of course I am also interested in whether there is a better approach.  If scripts are not best for executing c-style for-loops, does one need to do everything within a calculation field?  Using recursive functions? If so, I don't get why Filemaker does not make this easier by just having for loops more straight-forwardly available within calculation fields the way they have if functions available.

Apologies if this a really basic question and/or if it is in the wrong area within the forum.

I use filemaker 8 advanced (no option to select that in my profile). If this kind of thing is much simpler in new versions, please let me know.

Edited by 'makerphile
update application
Link to comment
Share on other sites

A script certainly can loop over selected records. In your example, you would probably want to start by creating a found set of the records you want to examine. This could be done either by performing a find (essentially just omitting the "X" record) or by Go to Related Record using a relationship that makes every record related to every record except itself (using the ≠ relational operator). And you would want to do this in a new window, so that you can easily return to the original record.

Once you have the found set you can do (pseudocode):

Go to Record [First]
Loop
 # set some variables (or global fields) using data from the current record
 Go to Record [Next; Exit after last]
End Loop

(An alternative way to loop over related/found records is to use the GetNthRecord() function from the context of record "X", increasing the recordNumber parameter on each iteration.)

Now you have all the data you need, from al the records you want, in variables and can return to the original record and do something with it. However, here comes the flaw in your proposed method: you want to calculate, for a single record, a total of 63 results; where do you intend to put them?  Surely you do not want to have 63 fields holding information about 63 other records?! No, the correct approach (at least for your example, if not for your real situation) is to put data from record "X" into global fields, and let each record calculate its own result by comparing the data from the global fields to its own data.

In addition to looping in a script, Filemaker also provides two ways to perform recursive calculations: a custom function that calls itself, and the While() function (you must have the Advanced version to install a custom function, and you need version 18 or higher to use the While() function). But once again, you need to ask yourself what to do with the result.

Also keep in mind that a calculation recalculates on its own, while a script must be called.

 

Edited by comment
Link to comment
Share on other sites

Thank you very much for the detailed reply, which helped me get off the ground scripting a for-loop-style implementation. I have tried the script approach along the model you laid out, though not yet using global fields.  I have used nested loops, the outer one cycling through each square X and the inner one cycling through each square to see whether its contents can reach square X, and if so, I append the contents/piece to the list of pieces in a "reachable_by_pieces" field for square X.

The script took a substantial amount of time and I am thinking for-loop-style implementations would run quicker using the while function or recursive functions pre-filemaker-18, for example to make reachable_by_pieces a calculation field.

Without the while function, would the main functions for implementing for-loops in calculations be let, evaluate, and getfield? 

Thanks

Link to comment
Share on other sites

3 hours ago, 'makerphile said:

I am thinking for-loop-style implementations would run quicker using the while function or recursive functions pre-filemaker-18

I doubt that, but since I don't know what exactly you're doing I cannot say for sure.

 

3 hours ago, 'makerphile said:

Without the while function, would the main functions for implementing for-loops in calculations be let, evaluate, and getfield? 

No. As I said, without the While function, you must use a custom function that calls itself. That's recursion, not a for loop.

 

Edited by comment
Link to comment
Share on other sites

Apologies, I am with you on the recursive custom functions.  I mean if I want to implement the functional equivalent of a for loop, I would need to have some kind of counter that increments each time I call the custom function, some sort of test within the custom function to see if the custom function should be called again based on whether the counter exceeds some value, and some kind of calculation to be carried out each time the function is called. Would the typical filemaker functions in the calculation engine that would be needed inside such a custom function be Let (e.g. to establish the counter and perhaps variables and values to be used in the calculation) and If (for the test)? Any other filemaker functions I should pay attention to, such as Evaluate? Thanks

Link to comment
Share on other sites

1 hour ago, 'makerphile said:

I would need to have some kind of counter that increments each time I call the custom function,

Not necessarily. In many implementations, the custom function will simply "reduce" the parameter it is processing - for example, if you are processing a string by its characters, you will call the next iteration with the string parameter reduced by 1 character from left or from right, until you run out of characters.

In cases where this is not feasible - such as in your example, where you want to process a set of records - you must add a counter parameter to the function. Beginners tend to add two parameters - a counter and a total count - and then make the recursive call like:

If ( counter < setSize ; MyCustomFunction ( paramA ; paramB ; ... ; counter + 1 ; setSize ) )

A more elegant approach uses only one parameter, initially populated by the total set size, and reduces it at each recursive call:

If ( setSize > 0 ; MyCustomFunction ( paramA ; paramB ; ... ; setSize - 1 ) )

and processes the set from last to first.

__
P.S. There is a very large repository of custom functions at https://www.briandunning.com/filemaker-custom-functions/ that you may use to look at some examples (just keep in mind that the quality varies).

 

 

Edited by comment
Link to comment
Share on other sites

This topic is 609 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.