Anyone got a zero fill (left) tcl proc or fragment?

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Anyone got a zero fill (left) tcl proc or fragment?

  • Creator
    Topic
  • #47795
    Rob Galino
    Participant

    Aloha all… I need to be able to check PID-3 (MRN) if it’s 6 digits or less, and if less, zero fill to the left to 6 digits.  If MRN is 6 digits or greater, no nothing…  If anyone has some a tcl proc or fragment I can use, it’s appreciated.

    Mahalo,

    – Rob

Viewing 4 reply threads
  • Author
    Replies
    • #56739
      Daniel Lee
      Participant

      There might be an easier way but here’s what I use:

       set newnum “000000”

       set newnum [append newnum [lindex $xlateInVals 0]]

       set newnum [string range $newnum [expr [string length $newnum] -6] [expr [string length $newnum] – 1]]

       set xlateOutVals [lreplace $xlateOutVals 0 0

        ]

    • #56740
      David Caragay
      Participant

      You might want to check out the format command.  

      hcitcl>set x “123456”

      hcitcl>set x1 ”  3456″

      hcitcl>set x2 “3456”

      hcitcl>set y [format “%06d” $x]

      hcitcl>echo $y

      123456

      hcitcl>set y [format “%06d” $x1]

      hcitcl>echo $y

      003456

      hcitcl>set y [format “%06d” $x2]

      hcitcl>echo $y

      003456

    • #56741
      Dennis Pfeifer
      Participant

      Just to provide the one liner that will fit in the preproc …

      set xlateOutVals

        ]]

        This will work fine until your numbers are larger than an integer …

        Dennis

      1. #56742
        Rob Galino
        Participant

        Used the following in a Pre Tcl:

        set xlateOutVals [format “%06s” $xlateInVals]

        Works like a charm, zero fills when MRN is 6 or less, does nothing if 6 or more…  Used “s” (string) instead of “d” as the “d” would output funky numbers if the MRN was already leading with zeros, see below…

        hcitcl>set x 0012

        hcitcl>set y [format “%06d” $x]      

        hcitcl>echo $y

        000010

        hcitcl>set x 0012

        hcitcl>set y [format “%06s” $x]

        hcitcl>echo $y

        000012

      2. #56743
        Tom Henderson
        Participant

        Just one word of caution about the format statement.  It sees numbers that start with a zero as octal numbers, so if you’re asking it to say, turn 0134 into 000134 it will bomb.  The solution is simple, though:  Just wrap the value with a [string trimleft 0] to first trim all the leading 0’s, if any, before formatting the number with the right number of leading zero’s.

        For example:

        set xlateOutVals [format “%06s” [string trimleft $xlateInVals 0]]

        If you’re sure the values will never have a leading zero, then it’s not necessary.  But if they ever might….

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

    Forum Statistics

    Registered Users
    5,127
    Forums
    28
    Topics
    9,299
    Replies
    34,443
    Topic Tags
    288
    Empty Topic Tags
    10