XLATE TCL / String Parse

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf XLATE TCL / String Parse

  • Creator
    Topic
  • #53445
    Jon Melin
    Participant

      Hello everyone. Still learning cloverleaf and I had a couple questions on manipulating a string. I have a long string with the information I need coming in one segment, and I need to take it apart within the XLATE and place each piece into it’s own segments. Here is an example:

      Input:

      Interval/Occurance Pattern: Monthly; Number of Occurances: >1; Ending Date (MM-DD-YY): 12-01-12; < In this case I need 3 individual words from the whole string ( Monthly, >1, 12-01-12). Each one will be placed in a different segment.

      I was trying to grab elements between the : and the ; but I was having trouble. Can anyone point me in the right direction in TCL within this XLATE?

      Thank you in advance.

    Viewing 2 reply threads
    • Author
      Replies
      • #77719
        Robert Kersemakers
        Participant

          Hi Jon,

          If you’re absolutely sure that the string will always come in this form, then you could use this code:

          Code:

          set input “Interval/Occurance Pattern: Monthly; Number of Occurances: >1; Ending Date (MM-DD-YY): 12-01-12; < "
          set pattern [string trim [lindex [split [lindex [split $input ";"] 0] ":"] 1]]
          set occurrence [string trim [lindex [split [lindex [split $input ";"] 1] ":"] 1]]
          set end_date [string trim [lindex [split [lindex [split $input ";"] 2] ":"] 1]]
          echo "; ; “

          A bit of explanation.

          First split the string on “;”. For the pattern, take the first list-element of the resulting list. Then you will need to split this first list-element on “:” and take the second list-element of the resulting list. For good show, I added a ‘string trim’ to get rid of excess white space.

          If you’re not sure of the order in which the string comes in, you will need to split it on “;” then split each list-element on “:” and check what’s in the first element of the resulting list, to determine what to do with the second element.

          Hope this helps to get you started.

          Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

        • #77720
          Tom Rioux
          Participant

            Here is another way you can go at it.

            In your xlate have your input source as the field that contains the value that you have listed as your input.  Have your output destination as three temporary variables (the naming of them is your choice):

            @pattern

            @occur

            @ending

            Here is what you would have as your pre tcl fragment:

            set invals [lindex $xlateInVals 0]    

            set vals [lsearch -all -regexp -inline $invals “;”]

            set vals [string map {; “”} $vals]

            set xlateOutVals $vals

            The first line will allow your to treat the variable invals as a string instead of a list.

            The second line will pull out the answers your are looking for.  Your string is also a searchable list.  Since all of your answers will end in a semicolon, you are able to search for the “;” and it will pull your three answers.

            The third line will strip off the semicolon from the end of each of your answers.

            The fourth line will place each of your answers in the temp variables listed in your destination.   From there you can move them anywhere you want in your message.   Remember that although xlateOutVals is a list, so is your “vals” variable.   Therefore, you don’t need the ”

              ” when setting your temp variables.

              I have tested this and it works for me.

              Hope this helps…

              Tom Rioux

          • #77721
            Jon Melin
            Participant

              Excellent. Thank you both for the help. That worked great.

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