Replace field back into a segment while using a foreach loop

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Replace field back into a segment while using a foreach loop

  • Creator
    Topic
  • #54208
    Penny Cummings
    Participant

    Help!  I am trying to loop through the OBX segment, change some data on the OBX.3 field then put OBX.3 back into the segment. I am using lreplace to replace $obxfield with $obx_3 in position 3  3.   When I run my code I get the error  ‘list doesn’t contain element 3’.    Here is my code:

    #loop through the message and convert OBX.3 to UPPERCASE

    foreach loc [lsearch -all -regexp $msg “^OBX”] {

    # get the obx segment and obx 3 field from the message

    set obxseg [lindex $msg $loc]

    set obxfield [split $obxseg $fsep]

    set obx_3 [lindex $obxfield 3]

    #make all data in obx.3 UPPERCASE

    regsub -all — {.*} $obx_3 {[string toupper {&}]} obx_3  

            set obx_3 [subst -nobackslashes -novariables $obx_3]

    ## rebuild segment (put new obx3 back into the segment)

    set newobx_3 [lreplace $obxfield 3 3 $obx_3]  

      set newobx_3 [join $newobx_3 $fsep]    

    #rebuild msg (put new segment back into the message)

    set newmsg [lreplace $msg $loc  $loc $newobx_3]

    set newmsg [join $newmsg “r”]

    msgset $mh $newmsg  

    };# close loop

Viewing 4 reply threads
  • Author
    Replies
    • #80605
      Robert Milfajt
      Participant

      Are you sure every OBX segment has at least 3 fields?

      Robert Milfajt
      Northwestern Medicine
      Chicago, IL

    • #80606
      Penny Cummings
      Participant

      Thank you!  I was able to figure that out but I now have another problem.  I am able to convert all 5 iterations of OBX.3 to upper case but when the message ‘CONTINUES’  only the last iteration is in the output.

      This is my code:

      #loop through the message and convert OBX.3 to UPPERCASE

      foreach loc [lsearch -all -regexp $msg “^OBX”] {

      # get the obx segment and obx 3 field from the message

      set obxseg [lindex $msg $loc]

      set obxfield [split $obxseg $fsep]

      set obx_3 [lindex $obxfield 3]

      #make all data in obx.3 UPPERCASE

      regsub -all — {.*} $obx_3 {[string toupper {&}]} obx_3  

      set obx_3 [subst -nobackslashes -novariables $obx_3]

      # rebuild segment (put new obx3 back into the segment)

      set newobxseg [lreplace $obxfield 3 3 $obx_3]

      set newobxseg [join $newobxseg $fsep]  

      #rebuild msg (put new segment back into the message)

      set newmsg [lreplace $msg $loc $loc $newobxseg]

      set newmsg [join $newmsg “r”]

      msgset $mh $newmsg  

      };# close loop

                 

      return “{CONTINUE $mh}”

      This is what the new segment looks like when I echo $newobxseg:

      OBX|1|NM|CONVERT TO UPPER||75|bpm|||||R|||20140505143841-0500

      OBX|2|NM|CONVERT TO UPPER||76|bpm|||||R|||20140505143841-0848

      OBX|3|NM|CONVERT TO UPPER||77|bpm|||||R|||20140505143841-0650

      OBX|4|NM|CONVERT TO UPPER||78|bpm|||||R|||20140505143841-4785

      OBX|5|NM|CONVERT TO UPPER||79|bpm|||||R|||20140505143841-3256

      BUT, this is the output with ‘CONTINUE’:

      CONTINUE: ‘MSH|^~&|||||20140505143841-0500||ORU^R01|10|P|2.3

      PID|||7000128^^^^EPIC~7000128^^^^PHHS||ONE^OBIX^||19840425|||||||||||340604813

      PV1|||^^LW04^1^^PHHS^^^^^^||||||||||||||||613600366|||||||||||||||||||||||||20140505143841

      ORC||||||

      OBR|||||||20140505143841

      OBX|1|NM|convert to upper||75|bpm|||||R|||20140505143841-0500

      OBX|2|NM|convert to upper||76|bpm|||||R|||20140505143841-0848

      OBX|3|NM|convert to upper||77|bpm|||||R|||20140505143841-0650

      OBX|4|NM|convert to upper||78|bpm|||||R|||20140505143841-4785

      OBX|5|NM|CONVERT TO UPPER||79|bpm|||||R|||20140505143841-3256

      I must be missing something in my code but I don’t know what.

    • #80607
      David Barr
      Participant

      You aren’t ever updating your $msg variable, so when do you the lreplace on $msg with your last segment it is throwing away the values that you put into $newmsg for the prior segments. Also msgset should be after the “close loop”.

    • #80608
      Brandon Grudt
      Participant

      Try this.  Not sure if I missed anything…

      #loop through the message and convert OBX.3 to UPPERCASE

      foreach loc [lsearch -all -regexp $msg “^OBX”] {

      # get the obx segment and obx 3 field from the message

      set obxseg [lindex $msg $loc]

      set obx_3 [lindex [split $obxseg $fsep] 3]

      #make all data in obx.3 UPPERCASE

      regsub -all — {.*} $obx_3 {[string toupper {&}]} obx_3  

      set obx_3 [subst -nobackslashes -novariables $obx_3]

      # rebuild segment (put new obx3 back into the segment)

      set obxseg [join [lreplace $obxseg 3 3 $obx_3] $fsep]

      #rebuild msg (put new segment back into the message)

      set msg [join [lreplace $msg $loc $loc $newobxseg] r]

      };# close loop

         

      msgset $mh $msg          

      return “{CONTINUE $mh}”

    • #80609
      Penny Cummings
      Participant

      Thank you both!   I did finally get it to work.  When doing the lreplace, I had to put the new segment back into the original $msg variable then set newmsg to $msg since I am working with two lists within a loop. And, I had to close the loop before the final join.   I hope this all make sense. I am posting the last part of my code where I made the changes.

      #rebuild msg (put new segment back into the message)

      set msg [lreplace $msg $loc $loc $newobxseg]

      set newmsg $msg

      };# close loop

      set newmsg [join $newmsg “r”]

      msgset $mh $newmsg  

      return “{CONTINUE $mh}”

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