1 2 Previous Next 25 Replies Latest reply on Aug 12, 2017 10:57 PM by babem

    Case statements with multiple results

    nkolios

      does anyone know if it is possible to carry out more than one procedure for any one case.

       

      Something like;

       

      Case( x=3; do this and do that;

      x=5; do something and do something else

      )

       

       

      Thanks

       

      Niko

        • 1. Re: Case statements with multiple results

          Can you provide a less abstract example? You can have assigning being done via a let statement in your calculation but having a true example of what you are trying to achieve would better serve you.

          • 2. Re: Case statements with multiple results
            DavidJondreau

            Case() statements don't "do" anything, they simply return results. Script steps "do" stuff.

            • 3. Re: Case statements with multiple results
              jbante

              Scripts carry out procedures (imperative programming); calculations just return results (functional programming), with the quirky but hugely significant exceptions of plugin functions and using Let to set variables. One calculation, one result. We can create the effect of returning more than one result by concatenating multiple results into one with delimiters, but really it's still one returned result. So the way to think about the Case function is not

               

              Case (

              x = 3 ; do this and do that ;

              x = 5 ; do something and do something else

              )


              but rather

               

              Case (

              x = 3 ; return this ;

              x = 5 ; return that & ¶ & something else

              )

              1 of 1 people found this helpful
              • 4. Re: Case statements with multiple results
                BruceHerbach

                Unfortunatly Case statements are only available in the calculation engine.  So you can't use them to do things like Perform script.  As already stated you can return multiple items.  When I do this,  I like to use the list function.  So it would look something like this:

                 

                Case(

                    x = 3; return;

                    x = 5; List( returnthat;somethingelse; athird; andfourth)

                )

                 

                I find this easier then putting all of the & and required to create the list.

                 

                Bruce

                • 5. Re: Case statements with multiple results
                  reelsteve

                  Case (

                  x="red";

                  let ([ ScriptTrigger=BE_executeScript ("myscript"; solution.fmp12; x)]; 1 );

                  x="yellow".....so on

                  )

                   

                  * be_executescript (scriptname; target filemaker file; param)  is a plugin function (base elements).

                   

                  Not to muddy the water but given that a case return can be wrapped in a Let statement and the Let statement serves both the purpose of declaring variables and in doing so, provides a gateway to the calc engine and its access to functionality thru the use of plug in calls,

                  is it not at least partially accurate to think of the case statement as "doing something" in that it is both declaring a variable which, on its own, is an act beyond a mere return value, and in doing so, is at at the very least playing a "Paul revere" role in igniting action ?

                  I suppose the case is still just returning something.  That something , a let, is the ignition vehicle.

                   

                  I guess it's like returning a book to someone and that book - having a cold virus living on it- makes everyone in the house sick....who gets the credit for "making people sick"?

                   

                  I would agree that this is a stretch were it not for the fact that the Let statement, used as a gateway, though seemingly hacky, is a fundamental tool for developing in filemaker.

                  1 of 1 people found this helpful
                  • 6. Re: Case statements with multiple results
                    jbante

                    Just because we do something all the time doesn't mean it's a healthy norm. Using the Let function to set variables is common practice, but far from fundamental to FileMaker development. Any application that uses it could be modified to not use it reasonably easily. It feels "hacky" because it's very far from being a fundamental tool for building in FileMaker — it runs counter to the natural role of calculations, which is to return a result without any side effects on the application state. Most other programming languages have a much blurrier distinction between statements and expressions, but the clarity of the distinction in FileMaker is part of what makes it more approachable and therefore valuable. I use the Let function to set variables every day, but I do that with an understanding that I'm working against the grain or crossing the streams by doing so.

                     

                    Also, the Case function does not need the Let function to help it call a plug-in function, even if it wants to return a different result.

                    • 7. Re: Case statements with multiple results
                      nkolios

                      Hi Bruce,

                      Thanks for the response.  I am going to investigate this one, it looks like just wht I need.

                       

                      Niko

                      • 8. Re: Case statements with multiple results
                        Mike_Mitchell

                        You can have a Case statement return different results by nesting Case statements. Example:

                         

                        Case (

                         

                        x = 3 ;

                         

                             Case ( y = 1 ; "Dick" ; y = 2 ; "Jane" ; "Spot" ) ;

                         

                        x = 5 ;

                         

                             Case ( y = 1 ; "Jack" ; y = 2 ; "Jill" ; "Hill" )

                         

                        )

                         

                        You can, as several have pointed out, substitute literal results for plugin calls to have the statement "do" something instead of "return" something. But the key being yes, you can nest Case statements to "branch" the logic if you need that functionality.

                         

                        HTH

                         

                        Mike

                         

                        Edit: For syntax errors in the original. Too early. Need ... more ... caffeine ...

                        • 9. Re: Case statements with multiple results
                          taylorsharpe

                          jbante has a good explanation, but I do want to add one thing about the case statement.  The case statement goes through each test in order and returns the first result where a test is true.  It does not keep looking for additional tests beyond the first successful one.  So for x = 2:

                           

                          Case (

                               x = 1 ; "fjdk" ;

                               x = 2 ; "abc" ;

                               x = 3 ; "lkdjsf" ;

                               x = 2 ; "29384' )

                           

                          This will return only the "abc".  It will never see the 4th test. 

                           

                          This functionality can be useful sometimes and a limitation other times.  I just wanted you to be aware of the designed behavior.

                          • 10. Re: Case statements with multiple results
                            nkolios

                            Thanks for the reply, good to know.

                            • 11. Re: Case statements with multiple results
                              nkolios

                              I looked at this and it doesn't quite do what I want it to.  What I want to try and do is set a global field to a value if a certain criteria is met but it doesnt look like case statements can handle that.

                              • 12. Re: Case statements with multiple results
                                nkolios

                                I haven't been able to play with this yet but it looks like it might work for me.

                                • 13. Re: Case statements with multiple results
                                  jbante

                                  You'll need a script to set a field to a value, not a calculation. The calculation will be useful to decide which value to set in that field, but you have to put that calculation in a Set Field script step.

                                  • 14. Re: Case statements with multiple results
                                    nkolios

                                    Can I simply just assign a variable with a value in this way or must i use a script trigger to do this?

                                    1 2 Previous Next