Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

Recommended Posts

  • Newbies
Posted

In a data table ("MASTER"), there are two three fields. These are:

     Key

     Text (with a variable number of words)

     Count (the number of words in Text; note these are different in different records)

 

In a different data table ("PORTAL RECORDS")I wish to create a new record for each word in the MASTER text field.

I created a layout with the three MASTER fields and a portal showing PORTAL RECORDS. PORTAL RECORDS has two fields: a key field (Key) and a text field (Word). They are linked by the Key field.

This script will go into the portal, create a new record, and set the word from Text that corresponds to the particular portal row in the WORD field:

     Go to Portal Row (Next), Exit after last (off)

     Set Field (  PORTAL RECORDS WORD; MiddleWords, Get (ActivePortalRowNumber); 1)

Everytime the script executes a new record is created in PORTAL RECORDS with the word corresponding to the portal row number. For example, if the 3rd word in TEXT is "dog" the third record created in PORTAL RECORDS will be "dog." However, if I try to set this up as a loop (i.e. with Go To Portal Row (Next), Exit After Last) it will continue creating new but blank records forever.

Is there a way to have another script execute this script the number of times in COUNT? For example, Perform Script the number of times in the Count field?  could set a variable or script parameter using Count, but how do I tell Perform Script to execute that many times? 

Any help is appreciated.

 

 

Posted

I typically don't mess iterating portals in this case I create records by setting a global field to the UUID that will create a 1:1 relationship which you can set the data across that. 

test.fmp12

in any loop you need to declare the max repetitions and then an iterator that would increment the repetition then an exit loop to prevent runaway script. 

 

  • Newbies
Posted

I don't understand your example, but it looks like the data that I need to separate (your
"a", "b", "c" etc.) is already in separate records. Am I misunderstanding this?

 

I've attached another solution, where I use the relationship between the active portal row number and the word count in the text field to control how many records are extracted (how many words are in the data field). This works well enough.

But I'm still curious how a variable can be used to control how many times a script executes.  

Data Extraction Test.fmp12

Posted (edited)
7 hours ago, Eryngium said:

I'm still curious how a variable can be used to control how many times a script executes.

Typically you would use two variables: one to set a limit to the number of iterations and one that increments on each iteration.

In your example, it could look like:

# CHECK IF RELATED RECORDS ALREADY EXIST
If [ IsEmpty ( Child::ParentID ) ]
   Set Variable [ $parentID; Parent::ParentID ] 
   Set Variable [ $text; Parent::Data ]
   Set Variable [ $n; WordCount ( $text ) ]
   Go to Layout [ Child ] 
   Loop
      Set Variable [ $i; $i + 1 ]
      Exit Loop If [ $i > $n ]
      New Record/Request
      Set Field [ Child::ParentID; $parentID ]
      Set Field [ Child::ExtractedWord; MiddleWords ( $text ; $i ; 1 ) ]
   End Loop
   Go to Layout [ original layout ] 
Else
   Beep 
End If


Often you can use a single variable for both purposes. If the order of the extracted words does not matter, you could start with the last word and decrease the $n variable until you run out:

# CHECK IF RELATED RECORDS ALREADY EXIST
If [ IsEmpty ( Child::ParentID ) ]
   Set Variable [ $parentID; Parent::ParentID ] 
   Set Variable [ $text; Parent::Data ]
   Set Variable [ $n; WordCount ( $text ) ]
   Go to Layout [ Child ] 
   Loop
      Exit Loop If [ $n = 0 ]
      New Record/Request
      Set Field [ Child::ParentID; $parentID ]
      Set Field [ Child::Extracted Word; MiddleWords ( $text ; $n ; 1 ) ]
      Set Variable [ $n; $n - 1 ]
   End Loop
   Go to Layout [ original layout ] 
Else
   Beep 
End If

 

---
Not directly related to your question, but I feel compelled to add:

  1. This method works entirely at the data level. It does not depend on the existence of a portal on the layout;
  2. This method does not require adding anything (fields, table occurrences, relationships) to the file's schema. The entire logic is contained in the script alone.

 

Edited by comment
Posted (edited)
10 hours ago, Eryngium said:

Is there a way to have another script execute this script the number of times in COUNT? For example, Perform Script the number of times in the Count field?  could set a variable or script parameter using Count, but how do I tell Perform Script to execute that many times? 

This is another way to approach the matter, without the use of variables at all, but instead utilizing native filemaker functions:

 

 

Skærmbillede 2024-06-12 kl. 12.06.59.png

master.fmp12

Edited by Søren Dyhr
Posted
51 minutes ago, Søren Dyhr said:

without the use of variables at all, but instead utilizing native filemaker functions:

One could say that the reason to use variables is to avoid the added complexity ...

  • Haha 1
Posted

Well repeaters as source does not allow the update option to work, but a Dummy table can then receive splitted repeaters into indivual records, and from the Dummy into the portal, now allows the update and adds remaining.

The scripts boils then down to 11 lines if the comments are omitted: 

Skærmbillede 2024-06-12 kl. 14.33.53.png

Posted (edited)
40 minutes ago, Søren Dyhr said:

The scripts boils then down to 11 lines

This is the way I look at it: if we are going to run a script (as of course we are in this case), then as much as possible let's make the script do everything that's necessary and avoid splitting part of the logic off to other resources. And I'd rather have the script contain a few more lines to complete the task on its own, instead of adding resources to the file's schema that serve no purpose other than this task.

 

Edited by comment
Posted (edited)

I’ve done stuff in this way for years, and find it much faster and more safe against recordlocking! But the present have a little flaw in not cleaning up the littering.

—sd

 

Edited by Søren Dyhr
Posted (edited)

The import in the dummy should be erased at some point, if say you have this script running 10000 times each day, would it stack up - without being at much use to anyone!

Yet another issue is, psos’ing as well as webdirect doesn’t support importing between internal tables that well!

Edited by Søren Dyhr
  • Newbies
Posted

Thanks to all who have commented. I'll be out of town until next week, but then I'll explore these approaches. I've been using FM since way back in the early 90s, but programming has never been my thing! 

Posted

If you're on a Mac OS version of Filemaker, could this be done instead - Which in my humble opinion is pretty fast and needs next to nothing to maintain.

Skærmbillede 2024-06-12 kl. 23.00.46.png

master.fmp12

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.