10 Replies Latest reply on Feb 10, 2012 11:13 PM by hopkins

    "Any" and "All" Custom Functions

    hopkins

      I use FileMaker Pro Advanced 9 and do not know if counterpart functions have been implemented in 10 or 11, but here are two custom functions I find useful:

       

      "Any" looks at related data and is true if any one or more meet criteria (soughtValue).

       

      If ( rFieldCount > 1 ;

      If ( GetNthRecord ( relatedField ; rFieldCount ) = soughtValue ;

      1 ;

      Any ( relatedField ; rFieldCount - 1 ; soughtValue ) ) ;

      If ( GetNthRecord ( relatedField ; rFieldCount ) = soughtValue ;

      1 ;

      0 ) )

       

      "Any" looks at related data and is true only if all meet criteria (soughtValue).

       

      If ( rFieldCount > 1 ;

      If ( GetNthRecord ( relatedField ; rFieldCount ) ≠ soughtValue ;

      0 ;

      All ( relatedField ; rFieldCount - 1 ; soughtValue ) ) ;

      If ( GetNthRecord ( relatedField ; rFieldCount ) ≠ soughtValue ;

      0 ;

      1 ) )

       

      I also have a "None" custom function but it is rather redundant as "not Any" does the same job.

       

      Here is an example file.

       

      Any who also find these useful, enjoy,

      Ted.

        • 1. Re: "Any" and "All" Custom Functions
          comment

          How about doing this non-recursively?

           

          Any =

           

          not IsEmpty ( FilterValues ( searchValue ; listOfValues ) )
          

           

           

          All =

           

          ValueCount ( FilterValues ( listOfValues ; searchValue ) ) = ValueCount ( listOfValues )
          
          • 2. Re: "Any" and "All" Custom Functions
            hopkins

            You are right Michael,

             

            These do the job, although I think

             

            If ( Any ( Paintbox ; Paints ; "red" ); "contains red" ; "red missing" )

             

            may be easier to read and understand than

             

            If ( not IsEmpty ( FilterValues ( "red" ; Paintbox ) ) ; "contains red" ; "red missing" ).

             

            I'll think on this though as I come to places where I want to distinguish "any" or "all,"

             

            Thank you,

            Ted.

            • 3. Re: "Any" and "All" Custom Functions
              RayCologon

              hopkins wrote:

              ...These do the job, although I think

               

              If ( Any ( Paintbox ; Paints ; "red" ); "contains red" ; "red missing" )

               

              may be easier to read and understand than

               

              If ( not IsEmpty ( FilterValues ( "red" ; Paintbox ) ) ; "contains red" ; "red missing" ).

               

              Hi Ted,

               

              If that's a concern there's no reason why syntax such as Michael suggested could not be re-worked to serve as the expression within a custom function - the point being that simplicity of code yields multiple benefits.

               

              Regards,

              Ray

              ------------------------------------------------

              R J Cologon, Ph.D.

              FileMaker Certified Developer

              Author, FileMaker Pro 10 Bible

              NightWing Enterprises, Melbourne, Australia

              http://www.nightwingenterprises.com

              ------------------------------------------------

              • 4. Re: "Any" and "All" Custom Functions
                Vaughan

                hopkins wrote:

                 

                I think

                 

                If ( Any ( Paintbox ; Paints ; "red" ); "contains red" ; "red missing" )

                 

                may be easier to read and understand than

                 

                If ( not IsEmpty ( FilterValues ( "red" ; Paintbox ) ) ; "contains red" ; "red missing" ).

                 

                Only for the developer that is familiar with the Any() custom function -- which might only be you. Everybody else who works on the system will be scratching their head. You'll be scratching your head whenever you work on anybody else's system that uses the native commands.

                • 5. Re: "Any" and "All" Custom Functions
                  comment

                  While I agree with both Ray and Vaughan, here's a third option:

                   

                  If (

                  // NONE OF THE LISTED COLORS IS RED:

                  IsEmpty ( FilterValues ( "red" ; List ( Paintbox::Color ) ) ) ;

                  "red missing" ;

                   

                  // OTHERWISE:

                  "contains red"

                  )

                  • 6. Re: "Any" and "All" Custom Functions
                    jbante

                    I second Michael's third option. While custom functions have their value for readability, and as one means to satisfy the DRY principle, it isn't the only means to accomplish either. Michael's example demonstrates commenting to improve readability. For applying the DRY principle to simple calculations like this, I find myself gravitating towards macro expansion tools rather than custom functions for reasons already mentioned in this thread and others. For example, "fmany" (read "fm-any") might expand to:

                     

                    /* Any value matches */ not IsEmpty ( FilterValues ( search ; List ( field ) ) )

                     

                    I find myself reserving custom functions for calculations that are particularly gnarly (like CustomList), particularly ubiquitous (like name-value pairs, for some of us), or require recursion (like sorting).

                    • 7. Re: "Any" and "All" Custom Functions
                      hopkins

                      Thank you Ray.

                       

                      Ted.

                      • 8. Re: "Any" and "All" Custom Functions
                        hopkins

                        Thank you Vaughn.

                         

                        Ted.

                        • 9. Re: "Any" and "All" Custom Functions
                          hopkins

                          Further thanks Michael,

                           

                          You, Vaughn and Ray contribut worthwhile thoughts.

                           

                          Ted.

                          • 10. Re: "Any" and "All" Custom Functions
                            hopkins

                            Thank you jbante.

                             

                            Also worth my while to consider.

                             

                            Ted.