XLT Questions using TCL

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf XLT Questions using TCL

  • Creator
    Topic
  • #53291
    Jon Melin
    Participant

      Hello. Still learning Cloverleaf so hopefully I can find some additional help here. I am looking to strip zeros off the front on of an ID (that’s always 8 digits long) but I also need to add dashes in the format of [ xxxx-xxx-x ].

      So it comes in as an ID of 000XXXXXXXX and I need to change it to XXXX-XXX-X. Currently I have written a small amount of TCL within my XLT to add zeros:

      Like this:

      set ptInVals [lindex $xlateInVals 0]

             # Format PID.3 to always be 11 digits, Right justified, zero-filled

             set xlateOutVals

        ]

        But I need to go the opposite direction and add dashes at those specific increments. Please let me know if you have any suggestions.

        Thank you,

        Jon

      Viewing 3 reply threads
      • Author
        Replies
        • #77172
          Chris Williams
          Participant

            Hi Jon,

            Format doesn’t work quite the way you tink. “%11s” will PAD the value with leading spaces. “%011s” will PAD with leading zeros rather than spaces. However it won’t trim the leading zeros. To do that you need to use [string trimleft …]. You can insert the dashes by using [regsub …]
            # Grab the initial value.
            [code]# Grab the initial value.

          • #77173
            Levy Lazarre
            Participant

              Hi Jon,

              This can also be done with string commands, if you don’t wish to use regular expressions:

              Code:



              set ptInVals [lindex $xlateInVals 0]

              # Remove all leading zeros
              set ptInVals [string trimleft $ptInVals 0]

              # Add the dashes at desired places, using ranges and string concatenation

              set ptInvals [string range $ptInvals 0 3]-[string range $ptInvals 4 6]-[string range $ptInvals 7 7]

              # Return the value as a list
              set xlateOutVals [list $ptInVals]]

            • #77174
              Levy Lazarre
              Participant

                Another variant of Chris’s technique is to use the regular expression itself to trim the leading zeros:

                Code:



                set ptInVals [lindex $xlateInVals 0]

                regsub -all — {0.*(d{4})(d{3})(d)} $ptInVals  {1-2-3} ptInVals

                # Return the value as a list
                set xlateOutVals [list $ptInVals]]

              • #77175
                Jon Melin
                Participant

                  Thanks everyone. That helps a lot and I appreciate the input.

                  Jon

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