4 Replies Latest reply on Jun 30, 2014 10:44 AM by sccardais

    Looping through Found Sets

    sccardais

      Title

      Looping through Found Sets

      Post

           My question relates to the outline of a script suggested here to help divide my database into random cohorts of 1/12 total records.

           http://forums.filemaker.com/posts/35570589c1?commentId=295871#295871

           Records in my table are assigned one of 8 Categories (text field) and 6 sub-categories (calculation number result). I want to perform a repetitive function on each of the 48 combinations but I'm not sure how to structure the loop in the script step.

           Steps in the Script

             
      •           Find Category 1 AND sub-category 1, perform function
      •      
      •           Find Category 1 AND sub-category 2, perform function
      •      
      •           Find Category 1 AND sub-category 3, perform function
      •      
      •           etc.
      •      
      •            

           Repeating this for a total of 48 combinations of category and sub-categories seems tedious and like an ideal situation for a Loop.

           Is there a way to create a looping process to go through all 48 combinations without having to copy / paste every combination in a script that steps through each in sequence?

        • 1. Re: Looping through Found Sets
          philmodjunk

               I agree that a loop makes sense. (And you could have used Post a New Answer to continue this discussion in your original thread).

               But it will depend on what criteria you have to use for your finds and how you make it available to your script.

               You might, for example, assign criteria to a variable as a list: Set Variable [$Fruit ; value: List ( "Apple" ; "Pear" ; "Kiwi" ; "Grape" ) ]

               You can then use the get value function to extract one value at a time from the variable inside your list to specify one of these values as find criteria.

               For examples of scripted finds that could be adapted to this type of loop, you might examine this one: Scripted Find Examples

          • 2. Re: Looping through Found Sets
            sccardais

                 Wasn't sure about protocol - new post or Post New Answer .. thanks for info.

                 Re your most recent suggestion, are you suggesting I create two variables (Category - with 8 items) and (sub-category with 6 items) and script in such a way that it performs 48 finds with the other scripted actions between each?

                 I'd want the scripted Finds based on both variables - stepping through all 6 possible combinations of Category 1, first, then all 6 combinations of Category 2, etc.

                  

            Using example from link included in your post, I think my situation would require a nested loop in order to go through all 6 possible combinations of Category 1 before advancing to Category 2 and so on.  Do you agree? Hate to ask but can you illustrate how a nested loop (if that's the right idea) would work in the example below?

            Set Variable [$FruitList ; value: Globals::gFruitList ]

            Enter Find Mode []
            Loop
               Set Variable [$K ; Value: $K + 1 ]
               Set Field [YourTable::Fruit ; GetValue ( $FruitList ; $K) ]
               Exit Loop If [ $K > valueCount ( $FruitList ) ]
               New Record/Request
            End Loop
            Set Error Capture [on]
            Perform Find[]

                  

            • 3. Re: Looping through Found Sets
              philmodjunk

                   While many of the examples use a global field, you probably do not need one in your case. The global fields in those examples are used as a way for a user to manually select/enter search criteria while in Browse Mode that a script can then access while in Find Mode in order to set up search criteria.

                   In your case, there's no user inputting the search criteria so you don't need the global field. That's why my example used a calculation with a list function instead of a global field reference.

              Set Variable [$FruitList ; value: List ( "Apple" ; "Strawberry" ; "Grape" ; "Peach" ) ]
                   Set Variable [$Sources ; value: List ( "Imported" ; "Local" )

              Set Error Capture [on]
              Loop
                      Set Variable [$J ; $J + 1 ]
                      Exit Loop If [ $J > ValueCount ( $Sources ) ]
                      Set Variable [$K ; value ; 0 ]
                      Loop
                    Set Variable [$K ; Value: $K + 1 ]
                    Exit Loop If [ $K > valueCount ( $FruitList ) ]
                         Enter Find Mode []
                        
              Set Field [YourTable::Fruit ; GetValue ( $FruitList ; $K) ]

                         Set Field [YOurTable::Source ; GetValue ( $Sources ; $J ) ]
                    Perform Find[]

                    Put code here to process the found set produced by this find.

                     End Loop
              End Loop

                    

              • 4. Re: Looping through Found Sets
                sccardais

                     Thanks very much.

                     This worked perfectly.