TCL: Multiple conditions in IF statement

Clovertech Forums Read Only Archives Cloverleaf Tcl Library TCL: Multiple conditions in IF statement

  • Creator
    Topic
  • #51923
    Justin Largent
    Participant

      I’m looking for TCL code that supports multiple conditions within an IF statement.   Something similar to a simple C IF statement, for example.

      Like:

      if ( (a == b)  &&  (x == y  || x != a)  )  {

        do something;

      }

      I can’t imagine this is a shortcoming of TCL, but I haven’t seen an example of this syntax thus far.  Multiple braces or brackets haven’t worked either.

      Thanks in advance.

    Viewing 4 reply threads
    • Author
      Replies
      • #72316
        David Barr
        Participant

          Code:

          if { ($a == $b)  && ($x == $y || $x != $a)  }  {
           do something;
          }

          If your variables are strings instead of numbers, then you need to use “eq” and “ne” to compare instead of == and !=.

        • #72317
          Charlie Bursell
          Participant

            While the == and != will work for strings in Tcl the ne and eq syntax is much more ergonomic.

            However, your comparisons will never work A will never be equal to be

            Perhaps you meant to say:

            if { ($a == $b)  && ($x == $y || $x != $a)  }  {

             do something;

            }

            As Dave stated.  But the following is better

            if { ($a eq “$b”)  && ($x eq “$y” || $x ne “$a”)  }  {

             do something;

            }

            or conversley

            if { [string equal $a $b] &&

                         ([string equal $x $y] || ![string equal $x $a]]} {

                Do something

            }

            Lots of ways to do the same thing.  You could even use a regexp command

          • #72318

            Hi, Justin!!!!!

            I like to nest if’s instead of using “and”.

            if ($var eq “string”) {
            [code]if ($var eq “string”) {

            -- Max Drown (Infor)

          • #72319
            Justin Largent
            Participant

              Thanks guys!  This seemed like such a simple question, I knew it had to be “operator error”.  I had tried the parentheses initially and couldn’t get it to work, and found no reference for grouping syntax for IF statements in my searching.

              Your examples work just fine.  My example was just a C example showing the desired groupings, I’m not actually using those evaluations.

              Howdy Max!  The nested ifs would be ok, but I’m trying to build an outgoing email string for errors and it would have resulted in building the string multiple times (for readability).  I was just trying to make if a bit more elegant.  But hey, whatever works, right? 🙂

              Thanks again guys.

            • #72320
              David Barr
              Participant

                Charlie Bursell wrote:

                But the following is better

                if { ($a eq “$b”)

            Viewing 4 reply threads
            • The forum ‘Tcl Library’ is closed to new topics and replies.