######################################################################
# replaceHL7Field – generic routine to replace the data in an HL7 field
# Args: msg the entire HL7 message
# segmentID the name of the segment containing the field to replace
# fieldID the index of the field to replace
# data the data that will replace the current contents of the field
#
# Returns: the an entire HL7 message
#
proc replaceHL7Field {msg segmentID fieldID data {fieldSeparator “|”} } {
set segmentList [split $msg r]
set newSegmentList “”
foreach segment $segmentList {
if {[crange $segment 0 2] == $segmentID} {
set fieldList [split $segment $fieldSeparator]
set fieldList [lreplace $fieldList $fieldID $fieldID $data]
set segment [join $fieldList $fieldSeparator]
}
set newSegmentList [lappend newSegmentList $segment]
}
return [join $newSegmentList r]
}