Remove OBX segment based on two conditions

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Remove OBX segment based on two conditions

  • Creator
    Topic
  • #54594
    Penny Cummings
    Participant

    Hello,  I am working on a tcl proc to remove an obx segment if two conditions are met.  For example if the value of 2382 is present in any iteration of obx.3 (first condition) and the value of CO2RR is in any additional iteration of obx.3 (second condition), remove the obx segment that contains the value of CO2RR in obx.3.  The code I have will remove the segment based on the second condition. I have tried multiple ways to remove the segment based on two conditions to include nested IF’s  but I cant get it to work.  My code that works is based on the second condition and is below.

    # get message and field seperators

    set msg  [msgget $mh]        ;# get message handle

    set segments [split $msg “r”]    ;# split message into a list

    set fsep [csubstr $msg 3 1]    ;# get field separator

    set csep [csubstr $msg 4 1]  ;# get sub-separator

        set rmList {}

    # Loop through each OBX and save the location that needs to     be removed.

    foreach loc [lsearch -all -regexp $segments {^OBX}]  {

    set obxfield [split [lindex $segments $loc] $fsep]

    set obx_3 [lindex $obxfield 3]

    set obxsubfld [split $obx_3 $csep]

    set obx_3_1 [lindex $obxsubfld 0]

    #### (if value 2382 exits – evaluate the second if statement)####

    #if value CO2RR exits – remove segment with this value  

    if {$obx_3_1 eq “CO2RR”} {

    lappend rmList $loc

     }

     

    }

    # remove segment

    foreach loc [lsort -integer $rmList] {

        lvarpop segments $loc

    }

    msgset $mh [join $segments r]              

              return “{CONTINUE $mh}”

    Appreciate any help!! 😀

Viewing 3 reply threads
  • Author
    Replies
    • #82133
      James Cobane
      Participant

      Hi Penny,

      If I understand you correctly, you want to eliminate the OBX segment that contains CO2RR in OBX:3 if any OBX also contains 2382 in OBX:3.  i.e. if the 2nd OBX contains 2382 in OBX:3 and the 5th OBX contains CO2RR in OBX:3, you want to remove the 5th OBX.

      I would set a flag when you encounter an OBX that contains 2382 in OBX:3 (i.e. set myflag 1).  Then you can traverse the OBX’s and if OBX:3 contains CO2RR and myflag has been set, remove the OBX.

      Hope this makes sense.

      Jim Cobane

      Henry Ford Health

    • #82134
      Charlie Bursell
      Participant

      The flag is good Jim but it looks like she is saying the first condition could show up anywhere.  What if it showed up after the second condition.

      I would do two loops like:

      # Loop through each OBX and save the location that needs to be removed.

      # First look for OBX.3 value anywhere.  If found set flag and bailout

      set COND1 0

      foreach OBX [lsearch -all -inline -regexp $segments] {

         set fld3 [lindex [split [lindex [split $OBX $fsep] 3] $csep] 0]

         if {[string trim $fld3] eq “2382”} {

             set COND1 1

             break

         }

      }

      # If 2382 not found, nothing to do

      if {!$COND1} {return “{CONTINUE $mh}”}

      # Search in reverse order so we can pop off segments without affecting order

      foreach loc [lsort -integer -decreasing [lsearch -all -regexp $segments {^OBX}]]  {

         set fld3 [lindex [split [lindex [split $OBX $fsep] 3] $csep] 0]

         #if value CO2RR exits – remove segment with this value

         if {[string trim [string toupper $fld3]] eq “CO2RR”} {

             lvarpop segments $loc

         }

      }

      msgset $mh [join $segments r]

      return “{CONTINUE $mh}”

    • #82135
      Penny Cummings
      Participant

      Thank you both 😀   I will work on the code snip-it you supplied and let you know if it works.

    • #82136
      Lonnie Davis
      Participant

      I am actually working one this problem with Penny and by using Charlie’s code, I was able to “pop off” an OBX segment based on the needed conditions.  However, when it comes to renumbering those segments in OBX-1 I am just not getting it.

      The problem when I try coding this, is that it appears another loop is necessary to count the number of OBX segments after the lvarpop command and replace OBX-1 with a new count.  I was able to do so but get stuck on replacing the OBX segments in the original message with the new and renumbered segments.  I feel like there should be a way to renumber in the foreach loop that “pops off” the needed segment.

      After researching how to manage segment counters and renumbering OBX segments here on Clovertech, it seems this is something a fair amount of people struggle with.

      Any help with example code to renumber OBX segments after one is removed based on the above example code from Charlie would be very helpful.

      Thanks!

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

Forum Statistics

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