Looping issue in tcl proc

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Looping issue in tcl proc

  • Creator
    Topic
  • #53507
    Laurie Chaplen
    Participant

      Hi I’m new to tcl and I am trying to take a segment NK1 that looks like this:

      NK1|1|TEST^TWO^^||1570 NW PATRICK^^CORVALLIS^OR^97330^USA|(541)924-3801^^PH~(541)974-6780^^CP

      and change to

      NK1|1|TEST^TWO^^||1570 NW PATRICK^^CORVALLIS^OR^97330^USA|^PHN^PH^^541^9243801~^PHN^CP^^541^9746780

      This is the logic in my tcl proc:

                         set fieldList “”

                          if {[cequal $segid “NK1”]} {

                              set fieldList [split $segment |]

                              set nk15 [lindex $fieldList 5]

                              set nk15Flds [split $nk15 ~]

                                  foreach group $nk15Flds {

                                      set grpLst [split $group ^]

                                      set subfld0 [lindex $grpLst 0]

                                      set subfld1 [lindex $grpLst 1]

                                      set subfld2 [lindex $grpLst 2]

                                 }

                               set newSubfld  [concat ^PRN^$subfld2^$subfld0]

                                      set newsubfld0 [regsub {-} $newSubfld “”]

                                      set newsubfld01 [regsub {[)]} $newsubfld0 ^]  

                                      set newsubfld0f [regsub {[(]} $newsubfld01 ^]  

      echo “newSubfld = $newsubfld0f “

                               set newnk15Lst [lappend newnk15Lst $newsubfld0f]

      echo “newnk15List = $newnk15Lst”

                               set newnk15 [join $newnk15Lst ~]

                               set nk1Seglist [lreplace $fieldList 5 5 $newnk15]

                               set newnk1Seg [join $nk1Seglist |]

                               set msgseglist [lreplace $msgseglist $count $count $newnk1Seg]

                          }

      this is what I’m getting as a result… it appears to drop the first subfield :

      NK1|1|TEST^TWO^^||1570 NW PATRICK^^CORVALLIS^OR^97330^USA|^PRN^CP^^541^9746780

      Any help is very appreciated

    Viewing 2 reply threads
    • Author
      Replies
      • #77928
        Elisha Gould
        Participant

          instead of:

          set newnk15Lst [lappend newnk15Lst $newsubfld0f]

          it should just be:

          lappend newnk15Lst $newsubfld0f

          It is suggestable to grab the field, component, repeat, etc separators from the message rather than specify them directly.

          we generally use a global namespace to store the separators when we receive the message.

          ie something like the following grabs the separators, then they need to be stored in globals for use.

          set myFS [csubstr $myMsg 3 1]

          set myCS [csubstr $myMsg 4 1]

          set myRS [csubstr $myMsg 5 1]

          set myEC [csubstr $myMsg 6 1]

          set mySS [csubstr $myMsg 7 1]

          Also might be better off to have a separate proc to process the phone number and return a list with two elements.

          proc getNumber {aNum} {

          regsub — {-} $aNum “” myNum

          regexp — {((.*?))(.*)$} $myNum tmp areacode number

          return

            }

            Also for the field I would recommend using a list and joining with the component separator than entering them in for each time its used.

            ie

            set myNum [getNum $subfld0]

            set newSubfld [join

              [lindex $myNum 1]] $myCS]

              set newSubfld

          • #77929
            Robert Milfajt
            Participant

              Thought I’d add this before Charlie.   😀

              csubstr is going away and you should use string range.

              Code:


              set myFS [csubstr $myMsg 3 1]
              set myCS [csubstr $myMsg 4 1]

              would become

              Code:


              set myFS [string range $myMsg 3 3]
              set myCS [string range $myMsg 4 4]

              Bob

              Robert Milfajt
              Northwestern Medicine
              Chicago, IL

            • #77930
              David Barr
              Participant

                I would use the HL7 library that I’ve posted in the TCL section of this forum.

                Code:

                package require hl7
                set hl7 [hl7::parse_msg [msgget $mh]
                for { set i 1 } { $i <= [hl7::count hl7 NK1] } { incr i } {
                 for { set j 1 } { $j area exchange subscriber
                   hl7::set_field hl7 NK1($i).5($j).1 “”
                   hl7::set_field hl7 NK1($i).5($j).2 PHN
                   hl7::set_field hl7 NK1($i).5($j).5 $area
                   hl7::set_field hl7 NK1($i).5($j).6 “$exchange$subscriber”
                 }
                }
                msgset $mh [hl7::join_msg hl7]

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