Quick TCL question – Numbers to String

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Quick TCL question – Numbers to String

  • Creator
    Topic
  • #52802
    Rehman Masood
    Participant

      I need to validate that the SSN is a 9 digit number and my inputs can be

      Code:

      123456789 (valid SSN, need to pass)
      000000000 (dummy SSN, need to skip)
             0 (dummy SSN, need to skip)

      So far, here is the TCL proc I have

      Code:

      proc formatSSN { incomingSSN } {
      set outgoingSSN {}
      if { $incomingSSN ne “” && $incomingSSN ne “000000000” && $incomingSSN ne ”        0″ } {
      set outgoingSSN [join [list [string range $incomingSSN 0 2] “-” [string range $incomingSSN 3 4] “-” [string range $incomingSSN 5 8]] “”]
      }
      return $outgoingSSN
      }

      I am not well versed with TCL but is there is TCL function to convert string to number? If so what it is. I am wondering if I can convert string to number, then multiple it with 1, and check if the result has a length of 9, I should be good but can’t seem to find the correct function.

      Thank you so much.

    Viewing 4 reply threads
    • Author
      Replies
      • #75523
        Jim Kosloskey
        Participant

          Rehman,

          If you want to know the length use string length like this:

          set len [string length $incomingSSN]

          Then you can check $len or just ise string length in your if.

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

        • #75524
          Robert Milfajt
          Participant

            Not sure about your plan to multiply by 1 and check for 9 length.  What if you get SSN = 0123456789.  Not sure if this is even valid, but throwing it out there as possibility.  I think you best using some version of regexp here.

            Code:

            if {[regexp — {^[0-9]{9}$} $incomingSSN] && ![regexp — {^[0]{9}$} $incomingSSN]} …

            Hope this helps,

            Robert Milfajt
            Northwestern Medicine
            Chicago, IL

          • #75525
            Charlie Bursell
            Participant

              # Checks SSN is all digits is length 9 and not all zeroes

              # If you had dashes you could remove them sith the string map command before hand

              if {![string is digit -strict $ssn]  ||

                            [string length $ssn] != 9   ||

                            [string trim $ssn 0] eq “” }

                 reject

              }

            • #75526
              Rehman Masood
              Participant

                Thank you so much Jim, Robert and Charlie! I think Charlie’s solution is what I will go with (its short and smart).

              • #75527
                Robert Milfajt
                Participant

                  I would go with Charlie’s solution too, wish I would have thought of it!   😀

                  Robert Milfajt
                  Northwestern Medicine
                  Chicago, IL

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