1 2 Previous Next 23 Replies Latest reply on May 19, 2016 6:54 AM by Magnus Fransson

    Understand Let and other syntax

    ChrisJohnston

      I have only gotten used to writing Let syntax… I have to say I have never really been sure what it all means. I am hoping that asking these questions will make me understand and be clear. From the example show on this page, it seems that a Let function can have more than one parameter, but they must use the list notation surrounded by “[…]”. If it is one, you don’t need to, and can’t use the brackets, correct? I also have picked up that FileMaker can use semicolons or commas as the separator in the Let function and maybe even other functions, correct? I think I picked this up by accident. I will appreciate to hear any idea, opinion, input, or answers. Thanks.

        • 1. Re: Understand Let and other syntax
          erolst

          ChrisJohnston wrote:

          I have only gotten used to writing Let syntax… I have to say I have never really been sure what it all means.

           

          It is quite simple if you de-structure the syntax:

           

          Let ( // function; functions that take arguments (almost all) always start with an open paren

            [ // variable declaration section open delimiter; optional for a single assignment, mandatory for multiple

            someVar = someExpression ; // a combined variable declaration/assignment …

            someOtherVar = someOtherExpression // … optionally referencing previously declared variables

            // { ; any number of additional assignments }

            ] ; // closing delimiter; semicolon delimits declaration section from calculation section

          // calculation section, optionally using any or all declared variables

          ) // function's close paren

           

          So what it all boils down to is just an ordinary calculation that is embedded into an environment where you can define variables to use in that calculation:

           

          Let (

          declare vars ;

          calculate using vars

          )

           

          which helps you writing code that is more efficient (processing-wise) and productive (clear and self-documenting).

          1 of 1 people found this helpful
          • 2. Re: Understand Let and other syntax
            Markus Schneider

            simple example

             

            let

            (

             

            // first declare the local variables, if any. This part is optional

            //  ONE Variable has to be defined, the others are optional

            [

              MyVar1 = "whatever value";

              MyVar2 = "whatever value";

              MyVarX = "whatever value"

            ]

             

            ;

             

            // the calculation follows now..

            MyVar1 & " " & MyVar2  & " " & MyVarX

             

            )

             

             

            edit: Erolst was faster..

            1 of 1 people found this helpful
            • 3. Re: Understand Let and other syntax
              erolst

              Markus Schneider wrote:

              first declare the local variables, if any. This part is optional

              I'm afraid that's not correct; you must perform at least one assignment.

               

              Leaving the declaration section empty: Let ( ; "" )

               

              or only using an expression: Let ( "" ; "" )

               

              will both throw a syntax error; you need at least: Let ( a = "" ; "" )

              1 of 1 people found this helpful
              • 4. Re: Understand Let and other syntax
                DanielShanahan

                I agree with erolst and Markus.  I prepend my Let variables with an underscore, which helps me see them in the calculation.  I also have a text expansion (TextExpander) that puts the Let statement in a more viewable form (at least to me.

                 

                So, instead of this:

                 

                Let({[}var1=expression1{;var2=expression2...]};calculation)

                 

                I have this:

                 

                Let ( [

                  _var1 = "Something" ;

                  _var2 = "Something Else" ;

                  _var3 = Right ( "And something else again" ; 3 )

                ] ;

                 

                _var1 & " " & _var2 & " " & _var3

                 

                )  // end Let

                1 of 1 people found this helpful
                • 5. Re: Understand Let and other syntax
                  beverly

                  I'm with Daniel. I use leading underscore to find the variables easier. If if need to debug, I'll use the $variables, so that I can verify the assignments in Data Viewer (when the Let is called in data viewer!).

                   

                  I also have a preference to space the statements, as this may allow comments:

                   

                       Let (

                       [ _a = 112 // this is a comment telling me what 'a' might be

                       ; _b = 16 // this is another comment

                       //; _c = 6 // this line has a prepended comment, allowing me to quickly "disable" an assignment.

                       /* another comment to note that removing the leading "//", above, will "enable" the _c assignment */

                       ; $d = "this is text"

                       ; _result = ( _a * _b ) & " " & $d // I like to calculate here rather than in the final

                   

                       ]; _result // only what I want to return, so that I can choose from ANY of the above assignment, including a final calculation!

                   

                       ) // close out the Let and perhaps show an expected result (1792 this is text)

                   

                   

                  Note my preference for using the ";" at the beginning of each line (except first and last line or comments). I can just plain forget that little separator! I started putting at the beginning of the line, so I can quickly scan down and see where I might have missed one. This is only my preferences, you may use what you like!

                   

                  beverly

                  2 of 2 people found this helpful
                  • 6. Re: Understand Let and other syntax
                    DanielShanahan

                    beverly wrote:

                     

                    ...and perhaps show an expected result (1792 this is text)

                    I like this, Beverly!  Never occurred to me before but I think it could be very beneficial to let the developer know what is expected.

                    1 of 1 people found this helpful
                    • 7. Re: Understand Let and other syntax
                      Magnus Fransson

                      Hi all,

                       

                      Just to emphasize that the semicolons are separators I usually put them on their own rows. (Se illustration.) That way you are less likely to do the classic programmer error: Adding a semicolon at the end of the last row.

                       

                      Also, the Let() funktion alows for (rather complex) calculations on every variable defenition, using previous variables.

                       

                      With best regards Magnus Fransson.Calc example.png

                      1 of 1 people found this helpful
                      • 8. Re: Understand Let and other syntax
                        beverly

                        I cannot see your screenshot clearly. Can you copy the dialog text and paste, pretty please?

                         

                        semicolons are separators, yes!

                        own rows? like this?

                        ...

                        a = 1

                        ;

                        b = 3

                        ...

                         

                        I think I tried that at one time and needed the quick disable/enable, but I may have placed the "//" incorrectly for the above. So, I revised and started the line with the separator. (I do the same method in JavaScript, PHP and anywhere there's a 'separator'.)

                         

                        beverly

                        1 of 1 people found this helpful
                        • 9. Re: Understand Let and other syntax
                          Extensitech

                          This goes a bit beyond the original thread topic, but...

                           

                          If you're interested, and I think you might be, I did a blog about formatting calculations that talks, among other things, about positioning of separators. I'd be interested in your comments. Calculation Function Formatting | Extensitech

                           

                          Chris Cain

                          Extensitech

                          1 of 1 people found this helpful
                          • 10. Re: Understand Let and other syntax
                            Markus Schneider

                            You're correct (as ever). I should stop answering postings when the phone rings )-:

                            1 of 1 people found this helpful
                            • 11. Re: Understand Let and other syntax
                              Magnus Fransson

                              Hi beverly,

                               

                              You must have posted while I was composing my post.

                               

                              Working in isolation, I have not yet needed to comment out a complete declaration like that. Thus I didn't realize that shortcoming of my choosen method.

                               

                              Attaching a cropped version of the picture as well as text, below.

                               

                              With best regards Magnus Fransson.

                              Calc example 2.png

                              Let
                              (
                                  [
                                      $Föa_ID = 1
                                  ;
                                      FieldSep = "    "    // [TAB]
                                  ;
                                      RecordSep = "¶"
                                  ;
                                      As.tabell = Quote ( ~GetSQLFieldTable ( As__ArtikelSaldo::!As_ArtikelSaldo_ID ) )
                                  ;
                                      As.Ar_ID = Quote ( ~GetSQLFieldName ( As__ArtikelSaldo::!Ar_Artikel ) )
                                  ;
                                      As.Föa_ID = Quote ( ~GetSQLFieldName ( As__ArtikelSaldo::!Föa_FöretagsAdress ) )
                                  ;
                                      As.Lager = Quote ( ~GetSQLFieldName ( As__ArtikelSaldo::As811_Tillgängligt_B ) )
                                  ;
                                      Ar.tabell = Quote ( ~GetSQLFieldTable ( Ar__Artikel::!Ar_Artikel_ID ) )
                                  ;
                                      Ar.Ar_ID = Quote ( ~GetSQLFieldName ( Ar__Artikel::!Ar_Artikel_ID ) )
                                  ;
                                      Ar.Give = Quote ( ~GetSQLFieldName ( Ar__Artikel::Ar51_Giveaway_YesNo ) )
                                  ;
                                      Ar.Lev = Quote ( ~GetSQLFieldName ( Ar__Artikel::Ar52_Giveaway_Leverantor ) )
                                  ;
                                      SQL = "SELECT " & Ar.Lev & ", " & Ar.Ar_ID & " FROM " & Ar.tabell & " JOIN " & As.tabell & " ON " & Ar.tabell & "." & Ar.Ar_ID & " = " & As.tabell & "." & As.Ar_ID & " WHERE " & As.tabell & "." & As.Föa_ID & " = ? AND " & Ar.tabell & "." & Ar.Give & " = 1 AND " & As.tabell & "." & As.Lager & " >= 1"
                                  ;
                                      Error = EvaluationError ( ExecuteSQL ( SQL ; FieldSep ; RecordSep ; $Föa_ID ) )
                                  ;
                                      Result = Case( not Error ; ExecuteSQL ( SQL ; FieldSep ; RecordSep ; $Föa_ID ) )
                                  ]
                              ;
                                  //”SQL: " & SQL & "¶Error: " & Error & "¶Result:¶" &
                                  Result
                              )
                              
                              1 of 1 people found this helpful
                              • 12. Re: Understand Let and other syntax
                                dtcgnet

                                One way in which Let statements can make your processing more efficient is that you can reduce the number of evaluations that must be made.

                                 

                                For instance (and both methods are just for illustration):

                                 

                                This method makes FileMaker evaluate "Get ( CurrentDate )" 10 times.

                                Case (

                                Get ( CurrentDate ) = "1/1/2021" ; Get ( CurrentDate ) + 1 ;

                                Get ( CurrentDate ) = "1/1/2022" ; Get ( CurrentDate ) + 2 ;

                                Get ( CurrentDate ) = "1/1/2023" ; Get ( CurrentDate ) + 3 ;

                                Get ( CurrentDate ) = "1/1/2024" ; Get ( CurrentDate ) + 4 ;

                                Get ( CurrentDate ) = "1/1/2025" ; Get ( CurrentDate ) + 5 ;

                                "")

                                 

                                Whereas, using a Let, "Get ( CurrentDate )" is evaluated only one time:

                                Let (

                                x_currentdate = Get ( CurrentDate ) ;

                                x_currentdate + Right ( x_currentdate ; 1 )

                                )

                                 

                                Also, in my example, I've started the variable created by the Let with "x_". Just personal preference, like Daniel and Beverly. I start with an "x_" because that way I don't have to escape quote marks if I use ExecuteSQL in any way.

                                1 of 1 people found this helpful
                                • 13. Re: Understand Let and other syntax
                                  Magnus Fransson

                                  Hi Extensitech

                                   

                                  I use a combination of pushing left parenthesis "(" to a new row and indentation to help me associate rows of code.

                                  I prepared a illustration.

                                   

                                  With best regards Magnus Fransson.

                                  1 of 1 people found this helpful
                                  • 14. Re: Understand Let and other syntax
                                    erolst

                                    Markus Schneider wrote:

                                    You're correct (as ever).

                                    Thanks for the flowers, but I know for a fact that that isn't correct, either … 

                                    1 of 1 people found this helpful
                                    1 2 Previous Next