TCL code to kill msg based on subfield comparison

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf TCL code to kill msg based on subfield comparison

  • Creator
    Topic
  • #49173
    Duy Nguyen
    Participant

      I want to kill a message if the last name in PID segment, field 5, subfield 0 (last name subfield) is equal to any variation of “test” i.e. TEST, Test, test.

    Viewing 22 reply threads
    • Author
      Replies
      • #60974
        David Gordon
        Participant

          Why not just uppercase the field and compare it that way?

          so:

          Code:

          if {[string equal [string toupper $subField] “TEST”]} {
           #Do whatever
           }

        • #60975
          Keith McLeod
          Participant

            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.

          • #60976
            Duy Nguyen
            Participant

              Keith McLeod wrote:

              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?

            • #60977
              David Gordon
              Participant

                That’s the way I’d do it.  You could nest them to make it one line, but no big deal:

                Code:

                  set subfield [lindex [split $field $fieldSep] 0]

              • #60978
                Keith McLeod
                Participant

                  You probably want index 4 for the ^ to grab the component separator.

                • #60979
                  Duy Nguyen
                  Participant

                    Keith McLeod wrote:

                    You probably want index 4 for the ^ to grab the component separator.

                    like this?

                    set subfield [lindex [split $field 4] 0]

                  • #60980
                    Keith McLeod
                    Participant

                      for your field and component separators:

                      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…

                    • #60981
                      Duy Nguyen
                      Participant

                        Keith McLeod wrote:

                        for your field and component separators:

                        set fieldSep [string range $msg 3 3]

                      • #60982
                        Charlie Bursell
                        Participant

                          Boy, you guys haven’t sat in on one of my Tcl classes lately, have you?  ðŸ˜€

                          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  ðŸ˜‰

                        • #60983
                          Duy Nguyen
                          Participant

                            Charlie Bursell wrote:

                            Boy, you guys haven’t sat in on one of my Tcl classes lately, have you?

                          • #60984
                            Charlie Bursell
                            Participant

                              You lefy off the else clause in your if statement

                            • #60985
                              Duy Nguyen
                              Participant

                                Charlie Bursell wrote:

                                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.”

                              • #60986
                                David Gordon
                                Participant

                                  Try using string match instead of string equal.  Match uses glob-style expressions and should only match ‘test’ exactly.  I think you can still use the -nocase argument.

                                • #60987
                                  Charlie Bursell
                                  Participant

                                    NO!  Do not use string match.  It is not called for here.

                                    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

                                  • #60988
                                    Scott Lee
                                    Participant

                                      Quote:

                                      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

                                    • #60989
                                      Duy Nguyen
                                      Participant

                                        Scott Lee wrote:

                                        Quote:

                                        CONTINUE: ‘MSH|^~&|QUEST|TDL||00050046|200702112258|TOPNET2NEON|ORU^R01|XO000565A|D|2.3|||||’

                                        [0:TEST] ‘kill_pid_5_0 ‘ returned bogus strMsgId ‘message0’

                                      • #60990
                                        Scott Lee
                                        Participant

                                          I wouldn’t say it is ‘correct’ until it is tested with good data.  I will say it is probably doing exactly what is written, for better or worse, on the test messages it was given.   😯

                                          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

                                        • #60991
                                          Duy Nguyen
                                          Participant

                                            Scott Lee wrote:

                                            I wouldn’t say it is ‘correct’ until it is tested with good data.

                                          • #60992
                                            Duy Nguyen
                                            Participant

                                              ######################################################################

                                              # 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

                                              }

                                            • #60993
                                              Scott Lee
                                              Participant

                                                Duy Nguyen wrote:

                                                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 your logic is correct, but you won’t know for sure until you test it thoroughly with good data.  It only picked up the ‘1’ as a separator because the data fed to it was bogus.

                                                Scott

                                              • #60994
                                                Duy Nguyen
                                                Participant

                                                  Scott Lee wrote:

                                                  I wouldn’t say it is ‘correct’ until it is tested with good data.

                                                • #60995
                                                  Duy Nguyen
                                                  Participant

                                                    Adding the code in red seemed to do the trick.  Would this potentially be unsafe to use?

                                                    ######################################################################

                                                    # 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]

                                                    ##force componentSep to ^

                                                    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

                                                    }

                                                  • #60996
                                                    Scott Lee
                                                    Participant

                                                      If the data coming from the vendor is correct, then this change will make no difference.

                                                  Viewing 22 reply threads
                                                  • The forum ‘Cloverleaf’ is closed to new topics and replies.