Using string length in a if statement within a translation

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Using string length in a if statement within a translation

  • Creator
    Topic
  • #54679
    Usman Azher
    Participant

    Hello,

    I am building a translation and need help with using a string length function within a if statement. I would like to look at PV1-44 on the input side and see if I get the date/time as YYYYMMDD or YYYYMMDDHHSS. If it comes back as YYYYMMDD I would like to concatenate 0000 so that I have YYYYMMDD0000 on the output side. Otherwise I would like to copy YYYYMMDDHHSS to the output side.

    Thank you

Viewing 4 reply threads
  • Author
    Replies
    • #82547
      Keith McLeod
      Participant

      How about using the format command?

      Example:

      hcitcl>format “%-012s” 20150515

      201505150000

      This will right pad with “0” to a length of 12 characters.  No math.  If already 12 characters.

      hcitcl>format “%-012s” 201505151234

      201505151234

      hcitcl>format “%-012s” 2015051512345678

      2015051512345678

    • #82548
      Jeff Dinsmore
      Participant

      To answer your original string length question, you can use something like:

      Code:



      set dateString 201505180648

      if { [string length $dateString] == 12 } {
       # string is 12 characters long
       # do your work here
      } else {
       # do something else
      }

      Or, you could use a switch if there are multiple conditions you may be interested in:

      Code:


      switch -exact [string length $dateString] {
       8 {
        # string is 8 characters long
        # do something
       }
       12 {
         # string is 12 characters long
         # do something else
       }
       16 {
         # string is 16 characters long
         # do yet another thing
       }
       default {
         # do the default thing
       }
      }

      Or, you can simply use it to see if your string has any length:

      Code:


      if { [string length $dateString] } {
       # string is not zero length
       # do your work here
      } else {
       # do something else
      }

      Jeff Dinsmore
      Chesapeake Regional Healthcare

    • #82549
      Usman Azher
      Participant

      I am sorry I do not have much experience with Tcl. Can you explain to me where in the translation I am supposed to insert the format command? Also, is this tcl proc going to be inside the translation or as a pre or post proc?

    • #82550
      Keith McLeod
      Participant

      You would use something like this in your xlate or use in an xlt proc and use in same location within the xlate.

    • #82551
      Russ Ross
      Participant

      If you want to play around with another possibility, there is also a DATECOPYOPT available in the xlate drop down that the COPY action is located in.

      Setting that option before copying your inbound date field to an outbound field can also be used effectively in some cases and perhaps in the one you described.

      Russ Ross
      RussRoss318@gmail.com

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

Forum Statistics

Registered Users
5,117
Forums
28
Topics
9,293
Replies
34,435
Topic Tags
286
Empty Topic Tags
10