Skip to content

Instantly share code, notes, and snippets.

@jamerfort
Created February 21, 2012 23:16
Show Gist options
  • Save jamerfort/1879725 to your computer and use it in GitHub Desktop.
Save jamerfort/1879725 to your computer and use it in GitHub Desktop.
This file adds a number at the end of a label in each NTE.3 value.
# Example message
set msg {MSH|^~\&|APP.....
NTE|1||TEMP: Room Temperature
NTE|2||SOURCE: Hand
NTE|3||NOTES: First
NTE|4||SPECIMEN SITE: Thumb
NTE|5||TEMP: Room Temperature
NTE|6||SOURCE: Hand
NTE|7||NOTES: Second
NTE|8||SPECIMEN SITE: dorsal hand
NTE|9||TEMP: Room Temperature
NTE|10||SOURCE: Hand
NTE|11||NOTES: Third
NTE|12||SPECIMEN SITE: middle hand}
# Intialize an array where the key will be the label (TEMP, SOURCE, etc.)
# and the value will be current count for that label.
array set note_labels {}
# get your separators
# Note that the segment separator should be changed to \r for real HL7 messages.
# It has to be \n for the example message above.
set segment_separator \n
set field_separator [string index $msg 3]
# keep an array of the segments in the message (for rebuilding later)
set segments {}
# loop through each segment
foreach segment [split $msg $segment_separator] {
# split out the fields for each segment
set fields [split $segment $field_separator]
# only process NTE segments
if { [lindex $fields 0] == "NTE" } {
# get the value of NTE.3
set nte_3 [lindex $fields 3]
# get the index of the first colon
set first_colon [string first ":" $nte_3]
# only do something if the first colon was found
if { $first_colon != -1 } {
# pull out the label (TEMP, SOURCE, etc.)
set label [string range $nte_3 0 $first_colon]
# Have we seen this label before?
if { ! [info exist note_labels($label)] } {
# No, so initialize to 0
set note_labels($label) 0
}
# increment the counter for this label by one
incr note_labels($label)
# add the freshly incremented counter before the colon
set nte_3 [string replace $nte_3 $first_colon $first_colon "$note_labels($label):"]
# replace NTE.3
set fields [lreplace $fields 3 3 $nte_3]
}
}
# rebuild the segment
lappend segments [join $fields $field_separator]
}
# rebuild the message
set msg [join $segments $segment_separator]
# print the message to see the results
puts $msg
############## Outputs: ###################
# MSH|^~\&|APP.....
# NTE|1||TEMP1: Room Temperature
# NTE|2||SOURCE1: Hand
# NTE|3||NOTES1: First
# NTE|4||SPECIMEN SITE1: Thumb
# NTE|5||TEMP2: Room Temperature
# NTE|6||SOURCE2: Hand
# NTE|7||NOTES2: Second
# NTE|8||SPECIMEN SITE2: dorsal hand
# NTE|9||TEMP3: Room Temperature
# NTE|10||SOURCE3: Hand
# NTE|11||NOTES3: Third
# NTE|12||SPECIMEN SITE3: middle hand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment