› Clovertech Forums › Read Only Archives › Cloverleaf › Cloverleaf › TCL code to kill msg based on subfield comparison
so:
if {[string equal [string toupper $subField] “TEST”]} {
#Do whatever
}
regexp -nocase {^test$} $subField
This should match any flave of test, TeSt, tesT, etc…. and returns a 1 for a match.
you could also use something like:
regexp -nocase {^test$} $subField
This should match any flave of test, TeSt, tesT, etc…. and returns a 1 for a match.
Thank you for all your efficient methods! I’m also in question of the RED code in my original post. I want to make sure I’m using a legitimate method of honing in on the subfield using the same field separator….would this work?
set subfield [lindex [split $field $fieldSep] 0]
You probably want index 4 for the ^ to grab the component separator.
like this?
set subfield [lindex [split $field 4] 0]
set fieldSep [string range $msg 3 3] ;# Something like ‘|’
set compSep [string range $msg 4 4] ;# Something like ‘^’
# $field is your name field that looks like
# lastname^firstname^middlename
#############################################
#Your code rewritten
#############################################
set msg [msgget $mh]
set segmentList [split $msg r]
set segment [lindex [lregexp $segmentList ^PID] 0]
# {PID||212222|||Test^Bob^L|||)
set fieldSep [string range $msg 3 3] ;# Something like ‘|’
set compSep [string range $msg 4 4] ;# Something like ‘^’
set fieldList [split $segment $fieldSep] ;# This contain PID fields as a list
# PID {} 2111000 {} {} {lastname^firstname^middlename}
## zoning in on PID-5
set field [lindex $fieldList 5]
# {lastname^firstname^middlename}
## zoning in on last name subfield
## not sure if this splits the fields into subfield
set subfieldList [split $field $compSep]
# {lastname firstname middlename}
set subField [lindex $subfieldList 0]
# {lastname}
if {[regexp -nocase {^test$} $subField]} {
return “KILL $mh”
}
Something like this above…
for your field and component separators:
set fieldSep [string range $msg 3 3]
string commands are much more ergonomic than any other commands. You should *ALWAYS* use them when you can. Regular expressions are extremely powerful for doing complex comparisons and parsing. But why bring a Mack truck when a pickup will do?
Simply use the nocase flag in the string equal command.
string equal ?-nocase? ?-length int? string1 string2
Also, for extracting a single character, I find the string index command better suited than the string range.
With all this said, it does not make any of us wrong, just different.
There are only two people that write good software, you and me. And sometimes I wonder about you
Boy, you guys haven’t sat in on one of my Tcl classes lately, have you?
You lefy off the else clause in your if statement
Thanks…but i’m still curious as to why it’s filtering “TEST1” when I’m trying for just “test.”
Note the output from your proc! It is returning a message for each segment. Note that all segments are continued except for the PID which contains test1 and it is KILL’ed as it should be.
I suspect your data file is not IAW with HL7. Maybe you have a newline at the end of each segment or something else weird is going on.
ITS HAS NOTHING TO DO WITH THE USE OR NON-USE OF THE STRING EQUAL COMMAND!
Also, you did not include the entire proc so there is no way to tell what is after what you posted.
It’s not your logic. You are spoofing yourself
CONTINUE: ‘MSH|^~&|QUEST|TDL||00050046|200702112258|TOPNET2NEON|ORU^R01|XO000565A|D|2.3|||||’
[0:TEST] ‘kill_pid_5_0 ‘ returned bogus strMsgId ‘message0’
KILL: ‘PID|1|08000815|XO000565A||TEST1^PATIENT^^^^||19560404|M|||||||||||965781278||||||||’CONTINUE: ‘ORC|RE||’
CONTINUE: ‘OBR|1||XO000565A|^^^10165^BASIC METABOLIC PANEL W/EGFR^|||200702041200|||||||200702050017|S|KREMPL GREG^^A^^^^||||^^|XO^DIAGNOSTIC LABORATORY OF OKLAHOMA^1001 CORNELL PARKWAY^OKLAHOMA CITY^OK^73108^RONNIE G. SCHLESINGER, M.D.|200702112200|||F|||||||’
CONTINUE: ‘OBX|1|TX|^^^25000000^GLUCOSE^||65|mg/dL|65-99|N|||F|||200702112200|XO|’
Take a step back and look at your incoming data. Each segment in the message has been parsed separately – The MSH was continued, the PID was killed, the OBR was continued, etc. Regardless of other errors in the code, it will not work as expected if it is only seeing one segment at a time.
Scott
CONTINUE: ‘MSH|^~&|QUEST|TDL||00050046|200702112258|TOPNET2NEON|ORU^R01|XO000565A|D|2.3|||||’
[0:TEST] ‘kill_pid_5_0 ‘ returned bogus strMsgId ‘message0’
In your test, if ‘the message’ is a PID segment then the first 5 characters of the message are ‘PID|1’. So your separators are a pipe, which is normal, and the number one (“1”). So when you are parsing the message down to the patient’s name, subfieldList is split on a 1. Which is the 1 in TEST1. So TEST becomes the first field in the list and matches perfectly with your string equal condition. That’s why the PID is killed and the other segments are continued.
When I am testing new code, I use lots of echo statements to tell me what the value of certain variables is. ‘echo $subField’ would have shown why your condition was true.
And, as Charlie mentioned, There could be something else before or after the code posted that does something else. And you are still missing an else statement.
Scott
I wouldn’t say it is ‘correct’ until it is tested with good data.
# Name: kill_pid_5_0
# Purpose:
# UPoC type: tps
# Args: tps keyedlist containing the following keys:
# MODE run mode (“start”, “run” or “time”)
# MSGID message handle
# ARGS user-supplied arguments:
#
#
# Returns: tps disposition list:
#
#
proc kill_pid_5_0 { args } {
keylget args MODE mode ;# Fetch mode
set dispList {} ;# Nothing to return
switch -exact — $mode {
start {
# Perform special init functions
# N.B.: there may or may not be a MSGID key in args
}
run {
# ‘run’ mode always has a MSGID; fetch and process it
keylget args MSGID mh
## grabbing PID segment
set msg [msgget $mh]
set segmentList [split $msg r]
set segment [lindex [lregexp $segmentList ^PID] 0]
set fieldSep [string index $msg 3]
set componentSep [string index $msg 4]
set fieldList [split $segment $fieldSep]
## zoning in on PID-5
set field [lindex $fieldList 5]
## zoning in on last name subfield
set subfieldList [split $field $componentSep]
set subField [lindex $subfieldList 0]
## checking the sub-field and killing if condition is MET
if {[string equal -nocase $subField test]} {
lappend dispList “KILL $mh”
} else {
lappend dispList “CONTINUE $mh”
}
}
time {
# Timer-based processing
# N.B.: there may or may not be a MSGID key in args
}
shutdown {
# Doing some clean-up work
}
}
return $dispList
}
So then my logic is wrong, then..because i’m not wanting to split on a “1” for the subfields..i want to split on a ” ^ ” ???
I think
Scott
I wouldn’t say it is ‘correct’ until it is tested with good data.
######################################################################
# Name: kill_pid_5_0
# Purpose:
# UPoC type: tps
# Args: tps keyedlist containing the following keys:
# MODE run mode (“start”, “run” or “time”)
# MSGID message handle
# ARGS user-supplied arguments:
#
#
# Returns: tps disposition list:
#
#
proc kill_pid_5_0 { args } {
keylget args MODE mode ;# Fetch mode
set dispList {} ;# Nothing to return
switch -exact — $mode {
start {
# Perform special init functions
# N.B.: there may or may not be a MSGID key in args
}
run {
# ‘run’ mode always has a MSGID; fetch and process it
keylget args MSGID mh
## grabbing PID segment
set msg [msgget $mh]
set segmentList [split $msg r]
set segment [lindex [lregexp $segmentList ^PID] 0]
set fieldSep [string index $msg 3]
set componentSep [string index $msg 4]
set componentSep ^
set fieldList [split $segment $fieldSep]
## zoning in on PID-5
set field [lindex $fieldList 5]
## zoning in on last name subfield
set subfieldList [split $field $componentSep]
set subField [lindex $subfieldList 0]
echo the value of subfield is####: $subField
## checking the sub-field and killing if condition is MET
if {[string equal -nocase $subField test]} {
lappend dispList “KILL $mh”
} else {
lappend dispList “CONTINUE $mh”
}
}
time {
# Timer-based processing
# N.B.: there may or may not be a MSGID key in args
}
shutdown {
# Doing some clean-up work
}
}
return $dispList
}