I am using a foreach loop on a list of segments to do some processing. I am trying to increment the foreach loop’s counter to skip certain segments. When ever i use the ‘incr countSegs1’ command I get an error in the testing module. The ‘incr countSegs2’ command does not cause an error because it is not the foreach loop’s counter. Here is my code. Any help would be appreciated.
######################################################################
# Name: stringFindReplaceField
#
# Purpose: If the OBR-4 field matches, this will search a specified field for a string and put a
# predefined string in another field in a differnt field in
# the same segment
#
# Args: tpc keyedlist containing the following keys:
# MODE run mode (”start”, “run” or “time”)
# MSGID message handle
# ARGS keyed list of user arguments containing:
# SEGID the name of the segment containing the field
# INDEX the HL7 index of the field to check
# #LABTYPE Type of lab in OBR 4 (if value contains spaces enclose in {})
# SEARCH String to search for (if value contains spaces enclose in {})
#######################################################################
proc stringFindReplaceFieldTst { args } {
keylget args MODE mode
switch -exact — $mode {
start {
#Preform Special Init Functions
}
run {
#Get Args and put them in variables
keylget args MSGID mh
keylget args ARGS.SEGID seg
keylget args ARGS.INDEX fieldno
#keylget args ARGS.LABTYPE labType
keylget args ARGS.SEARCH searchString
#Set normal variables
set countSegs2 0
set segmentCounter 0
set countIndex 0
set msg [msgget $mh]
set segmentList [split $msg “r”]
#Variables The OBR Segment
set obr “OBR”
set four 4
set bCult “MIC^BLD CULT SCREEN”
#set testsegment [getHL7Segment $msg $obr]
#set testfield [getHL7Field $testsegment $four]
#Test to see if this is the result you want to search
#This foreach statement makes sure each segment is checked for the desired string
foreach countSegs1 $segmentList {
if {[string match “$obr*” [lindex $segmentList $countSegs2]]} {
if {[string match “*$bCult*” [lindex $segmentList $countSegs2]]} {
incr countSegs1
incr countSegs2
while {[string match “$seg*” [lindex $segmentList $countSegs2]]} {
if {[string match “*$searchString*” [lindex $segmentList $countSegs2]]} {
#breaks down the segment, inserts the new text, puts it back together
set breakdown [split [lindex $segmentList $countSegs2] “|”]
set newSeg [lreplace $breakdown 2 2 $searchString]
set newSeg [join $newSeg “|”]
set segmentList [lreplace $segmentList $countIndex $countSegs2 $newSeg]
set msg [join $segmentList n]
}
incr countSegs1
incr countSegs2
}
}
}
incr countSegs2
}
echo “$msg n”
return “{CONTINUE $mh}”
}
shutdown {
#cleanup procedures
}
default {
error “Unknown mode ‘$mode’ in stringSearch proc”
}
}
}
Thank you,
Garrett