IF numeric comparisons

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf IF numeric comparisons

  • Creator
    Topic
  • #49155
    David Garcia
    Participant

      Hi everyone,

      I have a Tcl proc that calculates the difference in days between the current date and the discharge date. The proc is applied to a COPY action in an xlate and the result is saved in a temp variable and then tested in an IF action. I’m satisfied that the proc is working correctly.

      The plan was to test the temp variable in the translation and take action if the number of days is greater than 7. The problem is that the numeric comparison operators do not work as expected on the result.

      I ran a bunch of tests. Assuming @elapsed_days contains 16, the following IF expressions evaluate to true or false

      @elapsed_days > =7       false

      @elapsed_days gt =7      false

      @elapsed_days gt =07    true

      @elapsed_days == =16  false

      @elapsed_days eq =16   true

      Can anyone explain what is going on here?

      Thanks!

      Dave

    Viewing 3 reply threads
    • Author
      Replies
      • #60917
        James Cobane
        Participant

          David,

          I simulated the logic outlined in your posting within an Xlate, and had no issues with the numeric comparisons (> >= < etc.).

        • #60918
          Chris Williams
          Participant

            The results you are getting indicate you are comparing strings rather than numeric values.

            @elapsed_days > =7       false — numeric comparison of non-numeric values

            @elapsed_days gt =7      false  — 1 is not greater than 7

            @elapsed_days gt =07    true  — 1 is greater than 0

            @elapsed_days == =16  false  — numeric comparison of nonnumeric values

            @elapsed_days eq =16   true  — both strings are the same

            When you work with these as strings, it may be helpful to think of how they would sort as an alphanumeric list:

            07

            16

            7

            Chris

          • #60919
            David Garcia
            Participant

              Thanks for the helpful replies. If the value in xlateOutVals is represented internally as a string, that would explain my results. I don’t suppose there’s any way to explicitly cast the result as an integer? If not, I can come up with a work-around. Here’s my code.

              Quote:

              set dateArg [lindex $xlateInVals 0]

              # Insert a space between the date and time

              append dateString [crange $dateArg 0 7] ” ” [crange $dateArg 8 11]

              # Convert the date to seconds

              set dateSecs [clock scan $dateString]

              # Calc the number of days between the current date and the input date

              # 1 day = 86400 seconds

              set days [expr ([clock seconds] – $dateSecs) / 86400]

              set xlateOutVals

            • #60920
              Russ Ross
              Participant

                It is also worth noting that numbers that start with a 0 are treated as base 8 numbers and not base 10.

                For exapmle, try the follwing at the hcitcl prompt

                hcitcl>if { [expr 77 == 077] } { echo TRUE } else {echo FALSE}

                FALSE

                hcitcl>if { [expr 77 == 77] } { echo TRUE } else {echo FALSE}

                TRUE

                Here is an example of a generic TCL proc that I call within other procs like TPS of XLT procs to compare if 2 numbers are equal that gets around this problem:

                Code:

                # Begin Module Header ==========================================================
                #
                #——
                # Name:
                #——
                #
                # oth_nequal.tcl
                #
                #———
                # Purpose:
                #———
                #
                # Compare 2 numbers to see if they are equal.
                #
                #——–
                # Inputs:
                #——–
                #
                # n1 = fist number to use for comparison
                # n2 = second number to use for comparision
                #
                #———
                # Outputs:
                #———
                #
                # returns 1 if the two input numbers are equal
                # returns 0 if the two input numbers are not equal
                #
                #——-
                # Notes:
                #——-
                #
                # This is not a robust module.  
                # It is expected that the input strings are reasonable numbers like:
                #
                #     “28”
                # “+00028   ”
                # “-  028   ”
                # ”    28.10″
                #
                # Spaces and leading zeros will be handled but not bogus numbers
                #
                # TCL will freak out with a syntax error if you try to use expr
                # to compare 2 numbers if either of them have leading zeros, so
                # this module will strip off leading zeros before comparing the two input numbers.
                #
                #———
                # History:
                #———
                #
                # 2000.05.24 Russ Ross
                #          – wrote initial version
                #
                # End Module Header ============================================================

                proc oth_nequal { n1 n2 } {

                   #——————————————
                   # remove any spaces from the number strings
                   #——————————————

                   regsub -all “x20” $n1 “”  n1
                   regsub -all “x20” $n2 “”  n2

                   #—————————-
                   # trim plus sign if it exists
                   # and trim leading zeros
                   #—————————-

                   regsub {+*[0]*} $n1 “” n1
                   regsub {+*[0]*} $n2 “” n2

                   #—————————————
                   # keep minus sign and trim leading zeros
                   #—————————————

                   regsub {-[0]*} $n1 “-” n1
                   regsub {-[0]*} $n2 “-” n2

                   #———————————
                   # compare the numbers for equality
                   #———————————

                   if { [expr $n1 == $n2] } {
                       return 1
                   } else {
                       return 0
                   }

                }

                Russ Ross
                RussRoss318@gmail.com

            Viewing 3 reply threads
            • The forum ‘Cloverleaf’ is closed to new topics and replies.