Need last 8 characters of a variable length field

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Need last 8 characters of a variable length field

  • Creator
    Topic
  • #54174
    Gena Gill
    Participant

      I need to copy PID 18 to OBX 14, and strip out all but the last 8 characters in the process.  PID 18 will be variable length, and the characters of the string will be different.  

      I know how to trim leading zeros, and other leading characters when it’s not a variable length field, but cannot quite figure out how to trip all but the last 8 characters when the length will vary, but always be at least 8 characters.

    Viewing 4 reply threads
    • Author
      Replies
      • #80455
        Gena Gill
        Participant

          Here’s the TCL that I’m using:

          set num [lindex $xlateInVals 0]

          set date [string range $num 8 end-0]

          set xlateOutVals

            IF the number is 3330520140417, the result I’m getting is 40417.  It’s basically trimming off the first 8 characters rather than trimming all but the last 8 characters.  I need to get the 20140417.

        • #80456
          Jerry Tilsley
          Participant

            Gina,

            Prior to your string range command do:

            Set strLength [string length $num]

            Set startLoc [expr $strLenght – 8]

            Then use the $startLoc variable as the start location of the string range command.

            Hope this helps!

            Jerry

          • #80457
            Keith McLeod
            Participant

              string range $x end-7 end

            • #80458
              Gena Gill
              Participant

                Thanks everyone!  I got the correct values with this:

                set num [lindex $xlateInVals 0]

                set strLength [string length $num]

                set startLoc [expr $strLength – 8]

                set date [string range $num $startLoc end-0]

                set xlateOutVals

              • #80459
                Robert Kersemakers
                Participant

                  Many ways to do this, but Keith’s solution is the easiest/most common/efficient way. You can use the argument ‘end’ in many string commands to indicate the last character of a string, and ‘end-x’ to indicate a character x places from the end.

                  So this would be shorter:

                  Code:

                  set num [lindex $xlateInVals 0]
                  set date [string range $num end-7 end]
                  set xlateOutVals [list $date]


                  Or even:

                  Code:

                  set xlateOutVals [list [string range [lindex $xlateInVals 0] end-7 end]]

                  Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

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