Why do I need to double join a segment?

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Why do I need to double join a segment?

  • Creator
    Topic
  • #53847
    Brandon Grudt
    Participant

      As part of a project to strip as much sensitive data as we can, I’ve created an script that allows me to wipe multiple subfields.  My argument is {SUBS {PID.19.0}} -, but I can dump multiple period separated subfield locations into the single argument.

      The pertinent portion of my script:

      foreach swap $subs {

      set swapL [split $swap “.”]

      set swapsegL [split [lsearch -inline -all -regexp $segments [lindex $swapL 0]] $sep]

      set swapfield [split [lindex $swapsegL [lindex $swapL 1]] $sub]

      set newfield [join [lreplace $swapfield [lindex $swapL 2] [lindex $swapL 2]] $sub]

      set newseg [join [lreplace $swapsegL [lindex $swapL 1] [lindex $swapL 1] $newfield] $sep]

      set segments [lreplace $segments [lsearch $segments [lindex $swapL 0]*] [lsearch $segments [lindex $swapL 0]*] $newseg]

      }

      When I tested this the first time, my PID segment returned as a list, even though I had joined it by the separator.  I was able to fix it by modifying the above line to read:

      set newseg [join [join [lreplace $swapsegL [lindex $swapL 1] [lindex $swapL 1] $newfield] $sep] “”]

      It’s working now, but I was hoping to get clarification as to why I needed a second join to turn the PID segment into a string before placing it back into the segments var.  I’ve run into this before, and would like to understand why this happens.

      Thanks in advance!

    Viewing 3 reply threads
    • Author
      Replies
      • #79174
        David Barr
        Participant

          Get rid of your “-all” argument to lsearch. This is making lsearch return a list of matching segments instead of the first one. You’re then treating this list as a string when you pass it to split.

        • #79175
          David Barr
          Participant

            You also need to add a caret to your regex so that it only matches the segment name at the beginning of the segment and not within the data.

            set swapsegL [split [lsearch -inline -regexp $segments ^[lindex $swapL 0]] $sep]

            This code would be a lot simpler if you were using a message parsing library.

          • #79176
            Brandon Grudt
            Participant

              The -all was in there for repeating segments, not that this would commonly be used for any repeating segments, but I always like to build for as broad a purpose as possible.  And David, do you have any information on using a message parsing library?

              Thanks.

            • #79177
              David Barr
              Participant

                If you want to handle repeating segments you have to use a loop, like this:

                Code:

                foreach swap $subs {
                set swapL [split $swap “.”]
                foreach segno [lsearch -all -regexp $segments ^[lindex $swapL 0]] {
                set swapsegL [split [lindex $segments $segno] $sep]
                set swapfield [split [lindex $swapsegL [lindex $swapL 1]] $sub]
                set newfield [join [lreplace $swapfield [lindex $swapL 2] [lindex $swapL 2]] $sub]
                set newseg [join [lreplace $swapsegL [lindex $swapL 1] [lindex $swapL 1] $newfield] $sep]
                set segments [lreplace $segments $segno $segno $newseg]
                }
                }

                If I was going to use a library, I’d write it something like

                Code:

                foreach swap $subs {
                 lassign [split $swap “.”] seg field comp
                 for { set i 1 } { $i <= [hl7::count hl7 $seg] } { incr i } {
                   hl7::set_field hl7 ${seg}($i).$field.$comp ""
                 }
                }

                You can get the library here: https://usspvlclovertch2.infor.com/viewtopic.php?t=4886

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