5 Replies Latest reply on Oct 29, 2012 4:18 PM by philmodjunk

    Finding repetition number of a search string

    donjuancarlos_1

      Title

      Finding repetition number of a search string

      Post

           I am using global repeating variables in a script.  I need to find the repetition number of a string.  For example:

             
      •           $Colors has the values red, green, yellow, blue and orange in it.
      •      
      •           If I give Filemaker the string "blue"  I want it to return the number 4.

           Does anyone know how to do this?

            

        • 1. Re: Finding repetition number of a search string
          philmodjunk

               There are two ways to list multiple values in a variable, in repetitions where you can use standard array notation $Colors[1], $Colors[2] and so forth to refer to a specific repetition or you can use a list of value separated by return and then getValue($Colors ; 1 ) can be used. The list of values option needs to be kept in mind because some things are easier using functions like FilterValues and ValueCount when you need to work with such sets of multiple values.

               You want this as a script or a calculation?

               A looping script can compare values until the end is reached or you find it.

               A recursive custom function can also loop through the values.

               Will you be able to know how many repetitions hold values each time?

          • 2. Re: Finding repetition number of a search string
            donjuancarlos_1

                 I could do either script or calc but calc is probably preferred

                 I thought of loop but wondered if there was a more efficient way

                 I will know how many repetitions there are.

            • 3. Re: Finding repetition number of a search string
              philmodjunk

                   knowing the number of repetitions makes it easier. Otherwise a return separated list might be the better option.

                   If you have FileMaker Advanced, this custom function can return the matching repetition number:

                   //FindRep(Array ; Value ; R ; End )
                   // Array  : A repeating set of values
                   // Value : value to be matched
                   // R       : current repetition to be checked
                   // End    : max number of repetitions to be checked
                   //
                   Case ( R > 1 ; 0 ; // value didn't match any repetition
                             Array[R] = Value ; R ;
                            FindRep( Array ; Value ; R + 1 ; End )
                           )

              • 4. Re: Finding repetition number of a search string
                donjuancarlos_1

                     I see what you are doing there.  But my FileMaker (Advanced 12 for Windows) is hating "Array[R]".  It says it is expecting an operator (+, -, *, etc) after Array.

                • 5. Re: Finding repetition number of a search string
                  philmodjunk

                       I got past the syntax error and corrected a goof in my original version, but I am getting inconsistent results with the modified version:

                       /FindRep(Array ; Value ; R ; End )
                       // Array  : A repeating set of values
                       // Value : value to be matched
                       // R       : current repetition to be checked
                       // End    : max number of repetitions to be checked
                       //
                       Let ( $Array = Array;
                       Case ( R > R ; 0 ; // value didn't match any repetition
                                 $Array[R] = Value ; R ;
                                FindRep( $Array ; Value ; R + 1 ; End )
                               )

                       If I use a list of return separated values, however, I can get the custom recursive function to work:

                       //FindValue ( TheList ; value ; k )
                       //
                       // Returns the position of Value in the return separated list of values in TheList. Returns 0 if not found
                       Case ( IsEmpty ( TheList ) ; 0 ; // value not found
                                  GetValue ( TheList ; 1 ) = value ; k ;
                                  FindValue ( RightValues ( TheList ; ValueCount ( TheList ) - 1 ) ; value ; k + 1 )
                                )