trimleft on a numeric field

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf trimleft on a numeric field

  • Creator
    Topic
  • #53160
    Mary Bodoh-Stone
    Participant

      Hi,

      I am new to tcl.  What I am trying to do I figure should be fairly simple but I can’t figure it out….. I need the very first numeric digit in a field.  Am trying trimleft but it’s not working.  Whatever the digit is 1-9, I need it.  Field could be 3-4 digits, but I need the first one.Thanks.

      – Mary

    Viewing 12 reply threads
    • Author
      Replies
      • #76745
        Jim Kosloskey
        Participant

          Mary,

          string trimleft is for removing characters.

          If you want a certain single character then either

          string index where index of 0 is character one

          or

          string range where first and last are indexes to the character(s) you want (like for the first character would be 0 0).

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

        • #76746
          Mary Bodoh-Stone
          Participant

            Hi Jim,

             I tried this… no error but it’s not working either; thanks alot.

            set xlateOutVals [ list [ string range [ lindex $xlateInVals 0 ] 0 0 ] ]

            or

            set xlateOutVals [ string range [ lindex $xlateInVals 0 ] 0 0 ]

            Neither works; thanks.

          • #76747
            Jim Kosloskey
            Participant

              Mary,

              Are you sure your lindex of xlateInVals is returning what you want?

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

            • #76748
              Mary Bodoh-Stone
              Participant

                No…my input side is a value of 401 and my output value is the same.  I am expecting/wanting a 4.

              • #76749
                David Barr
                Participant

                  Code:

                  regexp {^0*([1-9])} [lindex $xlateInVals 0] -> digit
                  set xlateOutVals [list $digit]

                • #76750
                  David Barr
                  Participant

                    Actually it wasn’t clear what you want trimmed off the left side of the number. I assumed that it was zeros, but if you change the pattern to {^[^1-9]*([1-9])} it would trim anything except 1-9.

                  • #76751
                    Mary Bodoh-Stone
                    Participant

                      David,

                      I actually want the very first digit only.

                    • #76752
                      Mary Bodoh-Stone
                      Participant

                        I’m reading this and maybe I still haven’t made it clear.  For example, I have a value of 401 and I want to keep the 4; or 542 and I want to keep the 5.  Thanks.

                      • #76753
                        Paul Bishop
                        Participant

                          try:

                          set new_field [csubstr $orig_field 0 1]

                          or

                          set new_field [string range $orig_field 0 0]

                          either of these will grab the first character of your input field.

                          Paul Bishop
                          Carle Foundation Hospital
                          Urbana, IL

                        • #76754
                          Mary Bodoh-Stone
                          Participant

                            Thank you everyone; I have it working now.

                          • #76755
                            Robert Kersemakers
                            Participant

                              I am wondering though: can your inbound value also be ‘Hello123’? You would get ‘H’ in return with the last solution. Or do you need the ‘1’ to be returned in this case?

                              Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

                            • #76756
                              David Barr
                              Participant

                                I think it’s a terminology problem. In TCL, “trim” means to throw away or ignore characters at the beginning or end of a string. I think she just wants to return the first character of a string.

                              • #76757

                                The c-commands are legacy tcl commands. The better approach moving forward would be like this …

                                set xlateOutVals

                                  0 1]]

                                  Also, it is important to always treat xlateInVals and xlateOutVals as tcl list objects (and not string objects). Even when you are 100% sure that you inbound value will not contain a space, someone someday may clone your code and use it for values containing spaces.

                                  -- Max Drown (Infor)

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