Tcl proc to verify Numeric value

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Tcl proc to verify Numeric value

  • Creator
    Topic
  • #50650
    mike brown
    Participant

    Hi All

    I wrote this quick tcl script to pull a value out of a field where the length is 8 and I need to know if there is a way to verify the data returned is numeric only. Is there a way to do that?

    my tcl script :

    set out [lindex $xlateInVals 0]

    set len [string length $out]

    if [cequal $len “8”] {

    set out “Y”

    }

    set xlateOutVals $out

    any ideas?

Viewing 5 reply threads
  • Author
    Replies
    • #66965
      Jim Kosloskey
      Participant

      Mike,

      Have you looked at the string command?

      I believe it has a sub function to check for digits (numerics).

      There is also a ctype function but I believe string is the preferred function.

      email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

    • #66966
      Bob Richardson
      Participant

      Greetings,

        You may wish to try a regexp command that returns either a 1 or 0 depending on the results of the pattern match.  I was playing around with a regexp for all numeric when this post hit my inbox – see results below.

      Handles empty string as well.  Hope this helps.

      Read: from beginning of string to end of string all digits one or more are present –  BobR

      regexp -all {^[0-9]+$} $val

      tcl>set val 1234567890

      1234567890

      tcl>regexp -all {^[0-9]+$} $val

      1

      tcl>set val abc123bbb

      abc123bbb

      tcl>regexp -all {^[0-9]+$} $val

      0

      tcl>set val “”

      tcl>regexp -all {^[0-9]+$} $val

      0

      tcl>set val 123456789o   <– lower case letter oh

      123456789o

      tcl>regexp -all {^[0-9]+$} $val

      0

      tcl>set val 3

      3

      tcl>regexp -all {^[0-9]+$} $val

      1

    • #66967
      Michael Hertel
      Participant

      Quote:

      set out [lindex $xlateInVals 0]

      set len [string length $out]

      if [cequal $len “8”] {

      set out “Y”

      }

      set xlateOutVals $out

      We use ctype, also don’t forget to return xlateOutVals as a list.

      set out [lindex $xlateInVals 0]

      set len [string length $out]

      if [cequal $len “8”] && [ctype digit $out] {

      set out “Y”

      }

      set xlateOutVals

    • #66968
      Charlie Bursell
      Participant

      Stay away from the “c” commands provied by the extended Tcl library.  There are core commands that do the same thing which are much better

      Take a look at the string is command    

                 if {![string is digit $val]} {echo $val is not all digits or empty string}

      Note the above is true if all digits or the empty string.  If you do not want it to be true for the empty string, use the -strict flag

                 if {![string is digit -strict $val]} {echo $val is not all digits}

    • #66969
      Jim Kosloskey
      Participant

      Charlie,

      Thanks – I do not have my work laptop with me and I could not for the life of me remember the string subfunction for checking type.

      email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

    • #66970
      mike brown
      Participant

      Thanks all these are very good and all of them work, i am testing them all and will choose from my testing…

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

Forum Statistics

Registered Users
5,117
Forums
28
Topics
9,292
Replies
34,435
Topic Tags
286
Empty Topic Tags
10