Changed my mind. Decided to post code – nothing else going on 😀
Jim will tell you how to do without Tcl and you can decide which way to go
Did not test a lot as I don’t have your messages
==============================================
# Set canned OBX segnent to whatever
set OBX OBX|1||||||.|
# Split message into segments
set Segments [split $msg r]
# Get location of all OBR segments
set obrLoc [lsearch -all -regexp $Segments {^OBR}]
# Traverse in reverse order so as not to change OBR locations
foreach loc [lsort -integer -decreasing $obrLoc] {
# Assume no DG1
set dg1Loc -1
# List of all DG1 segments each time
foreach dg1Loc [lsearch -all -regexp $Segments {^DG1}] {
# We need DG1 *AFTER* the OBR
if {$dg1Loc < $loc} {continue}
# Got one after OBR – bail out
break
}
# No DG1 found – No mod to this OBR loop
if {$dg1Loc < 0} {continue}
# Get last one
# See if multiple DG1 and hget location of last one for the loop
while {[regexp {^DG1} [lindex $Segments [expr $dg1Loc + 1]]]} {
incr dg1Loc
}
# Incr location so we insert *AFTER* DG1
incr dg1Loc
set Segments [linsert $Segments $dg1Loc $OBX]
}
msgset $mh [join $segments r]
return “{CONTINUE $mh}”