set OBRpos [lsearch -all $segList OBR*]
set ORCpos [lsearch -all -regexp $segList "^ORC"] ;# ORC break downforeach ORCidx $ORCpos { set ORCSeg [lindex $segList $ORCidx] set ORCFlds [split $ORCSeg $fieldSep] set ORC_id1 [lindex $ORCFlds 1] set ORC_id2 [lindex $ORCFlds 2] set ORC_id3 [lindex $ORCFlds 3] set ORC_idSeg1 [split $ORC_id1 $compSep] set ORC_idSeg2 [split $ORC_id2 $compSep] set ORC_idSeg3 [split $ORC_id3 $compSep] set ORC_id1a [lindex $ORC_idSeg1 0] set ORC_id2a [lindex $ORC_idSeg2 0] set ORC_id3a [lindex $ORC_idSeg3 0] #OBR # # The problem was that the original code was looping through all OBR segments FOREACH ORC segment # What we really want is to find the NEXT OBR after each ORC # # So, we lsearch for the next OBR starting at the current ORCidx # position - using the -start option for lsearch # if { [set OBRidx [lsearch -regexp -start $ORCidx $segList "^OBR"]] >= 0 } { # Process only one OBR per ORC set OBRSeg [lindex $segList $OBRidx] set OBRFlds [split $OBRSeg $fieldSep] set OBR_id2 [lindex $OBRFlds 2] set OBR_id3 [lindex $OBRFlds 3] set OBR_id4 [lindex $OBRFlds 4] set OBR_idSeg2 [split $OBR_id2 $compSep] set OBR_idSeg3 [split $OBR_id3 $compSep] set OBR_idSeg4 [split $OBR_id4 $compSep] set OBR_id2a [lindex $OBR_idSeg2 0] set OBR_id3a [lindex $OBR_idSeg3 0] set OBR_id4a [lindex $OBR_idSeg4 0] set ORMCONTENT {} set ORMCONTENT "$ORC_id1a:$ORC_id2a:$ORC_id3a:$OBR_id2a:$OBR_id3a:$OBR_id4a" echo ORMCONTENT = $ORMCONTENT }}
ORMCONTENT = NW:2516511-5::2516511-5::CMPNXORMCONTENT = NW:2516511-6::2516511-6::CBCDORMCONTENT = NW:2516511-7::2516511-7::LPPR1ORMCONTENT = NW:2516511-8::2516511-8::TSH
set fieldSep [string index $msg 3]set compSep [string index $msg 4]set segList [split $msg r]foreach seg [lsearch -inline -all -regexp $segList {^(ORC|OBR)}] { set fieldList [split $seg $fieldSep] set segName [lindex $fieldList 0] foreach fieldNum { 1 2 3 4 } { set field [lindex $fieldList $fieldNum] set compList [split $field $compSep] set ${segName}_id${fieldNum}a [lindex $compList 0] } if { $segName eq "OBR" } { set ORMCONTENT "$ORC_id1a:$ORC_id2a:$ORC_id3a:$OBR_id2a:$OBR_id3a:$OBR_id4a" puts "ORMCONTENT = $ORMCONTENT" }}