Seeking help with tcl script

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Seeking help with tcl script

  • Creator
    Topic
  • #50765
    Tom Arrowsmith
    Participant

      Hello,

      I am trying to write a tcl script that will kill any A04 messages where the account field is empty. Because I am relatively new to programming, and very new to tcl, I generally start with a script which is similar and edit it to perform the desired new outcome.

      In this case, I am starting with a script that kills any messages except A03’s, when those non-A03 messages have a populated “when discontinued” timestamp.

      Here’s an example of the pertinent part of that code, which I hope to change:

      if {[cequal [crange $segment 0 2] MSH]} {

      set fieldList [split $segment $field_sep]

      set msh_9 [lindex $fieldList 8]

      set msh_9_list [split $msh_9 $sub_sep]        

         set msh_9_1 [lindex $msh_9_list 0]

         set msh_9_2 [lindex $msh_9_list 1]

         if {[cequal $msh_9_2 “A03”]} {

         set disposition “CONTINUE”

         }

      } elseif {[cequal [crange $segment 0 2] PV1]} {

      set fieldList [split $segment $field_sep]

      set pv1_45 [lindex $fieldList 45]

      if {[string length $pv1_45] == 0 }  {

      set disposition “CONTINUE”

      } elseif {[cequal $pv1_45 “”””]} {

      set disposition “KILL”

      I wish to change it to say (expressed in “pseudo” code):

      If message is an A04, check to see if the account field (PID-18.1) is null, if it’s null – kill the message.

      I am not familiar with how to make the “cequal” to be “not cequal”. I have found the “ne” operator, but am unsure whether to use that rather than somehow keeping the “cequal” functionality expressed negatively.

      I am thinking that the first half of this code snippet could be re-written somewhat like this (I am using “ne” here – and changing A03 to A04):

      if {[cequal [crange $segment 0 2] MSH]} {

      set fieldList [split $segment $field_sep]

      set msh_9 [lindex $fieldList 8]

      set msh_9_list [split $msh_9 $sub_sep]        

         set msh_9_1 [lindex $msh_9_list 0]

         set msh_9_2 [lindex $msh_9_list 1]

         if {ne $msh_9_2 “A04”]} {

         set disposition “CONTINUE”

      Then in the second half, I’ll have to change it to key in on the PID-18.1 ….. not sure I trust my expertise to do this, although it looks somewhat understandable to me ….

      I appreciate any help I can get!

      Tom Arrowsmith

      Inova Loudoun Hospital

    Viewing 7 reply threads
    • Author
      Replies
      • #67435
        Charlie Bursell
        Participant

          A lot of work to accomplish a lttle  ðŸ˜€

          set msg [msgget $mh]

          set fldSep  [string index $msg 3]

          set subSep [string index $msg 4]

          set segments [split $msg r]

          set msh [split [lindex $segments 0] $fldSep]

          set msgType [string map “$subSep _” [lindex $msh 8]]

          # If not A04 send it on

          if {![string equal $msgType ADT_A04]} {return “{CONTINUE $mh}”}

          # Assumes CL 5.4.1 or later

          set pv1 [split [lsearch -inline -regexp $segments {^PV1}] $fldSep]

          set pv1_45 [lindex $pv1 45]

          if {[string equal $pv1_45 {“”}]} {return “{KILL $mh}”}

          return “{CONTINUE $mh}”

        • #67436
          Robert Milfajt
          Participant

            To answer your question, the not equal would look like this:

            Code:

            if {![cequal $msh_9_2 “A04″]} {

            Good luck!

            Bob

            Robert Milfajt
            Northwestern Medicine
            Chicago, IL

          • #67437
            Charlie Bursell
            Participant

              C’mon Bob, don’t tell him to use the cequal command.  I’m trying to break those habits  ðŸ˜‰

            • #67438
              Tom Arrowsmith
              Participant

                Thank you both so much. I’m so much at the beginning of this – I am truly one who is dependant on looking at existing code and modifying it. Have not had enough experience yet!

                …but this is one way to get it, huh?

                Why is cequal not the prefered method?

                Thanks!

                Tom

              • #67439
                Rob Abbott
                Keymaster

                  While cequal still works, it has been replaced by “string equal” which is supposedly faster…

                  Rob Abbott
                  Cloverleaf Emeritus

                • #67440
                  Tom Arrowsmith
                  Participant

                    😳 OK, I hope you can bear with me… I realize that there may be “better” or more “efficient” ways to code and I hope to get there, eventually. My goal is to first learn how to modify the existing script – keeping it as close to the original as possible. I came into this job recently and inherited a bunch of scripts, most of which, I think, were written by McKesson when we went live a few years back – and another subset written by my predecessor by tweaking the McKesson scripts.

                    My logic tells me that I should learn the basics – (things like how to turn a positive statement negative) – make sure I can operate that way and as time goes on learn the “better” ways. It’s just a bit too much for me at this time.

                    For instance – As I did not give you the full code – I don’t know if I can just drop in what you gave me into the existing code – or if more of it will have to be re-written.

                    Let me put the entire scripts here – first, the one that kills any non-A03 message with a discharge date – then the one I am coming up with now to kill any A04’s with no account number:

                    First script – kill non-A03s with discharge date:

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

                    # Name: kill_nonA03wDCDate.tcl

                    # Description Kills a message where discharge date is not on A03 message.

                    #

                    # Assumptions This proc assumes that the message contains an MSH segment.  

                    # UPoC type: tps

                    # Args: tps keyedlist containing the following keys:

                    #       MODE    run mode (“start”, “run” or “time”)

                    #       MSGID   message handle

                    #       ARGS    user-supplied arguments:

                    #               There are no user supplied arguments

                    #

                    # Returns: tps disposition list:

                    #

                    #

                    proc kill_nonA03wDCDate { 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

                       # Here we retrieve the data from our original message

                       set msg [msgget $mh]

                       # Now we need to determine our field and subcomponent seperatoprs

                       set field_sep [csubstr $msg 3 1] ;# HL7 field separator    

                       set sub_sep [csubstr $msg 4 1] ;# HL7 subfield separator    

                       set disposition “KILL”

                         

                       # Here we spilt the original message into a list of the segments contained within

                       set segmentList [split $msg r]

                       foreach segment $segmentList {

                    if {[cequal [crange $segment 0 2] MSH]} {

                    set fieldList [split $segment $field_sep]

                    set msh_9 [lindex $fieldList 8]

                    set msh_9_list [split $msh_9 $sub_sep]        

                        set msh_9_1 [lindex $msh_9_list 0]

                        set msh_9_2 [lindex $msh_9_list 1]

                        if {[cequal $msh_9_2 “A03”]} {

                        set disposition “CONTINUE”

                        }

                    } elseif {[cequal [crange $segment 0 2] PV1]} {

                    set fieldList [split $segment $field_sep]

                    set pv1_45 [lindex $fieldList 45]

                    if {[string length $pv1_45] == 0 }  {

                    set disposition “CONTINUE”

                    } elseif {[cequal $pv1_45 “”””]} {

                    set disposition “KILL”

                    }

                    }    

                       }

                       lappend dispList “$disposition $mh”            

                    return $dispList                

                      }

                      time {

                    # Timer-based processing

                    # N.B.: there may or may not be a MSGID key in args

                      }

                      shutdown {

                    echo “kill_nonA03wDCDate is shutting down”

                      }

                      default {

                    error “Unknown mode ‘$mode’ in kill_nonA03wDCDate”

                      }

                    }

                    return $dispList

                    }

                       

                    ******************************************************

                    Second script – kill A04’s without account number:

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

                    # Name: kill_A04wNoAccount.tcl

                    # Description Kills A04 message where account number is null.

                    #

                    # Assumptions This proc assumes that the message contains an MSH segment.  

                    # UPoC type: tps

                    # Args: tps keyedlist containing the following keys:

                    #       MODE    run mode (“start”, “run” or “time”)

                    #       MSGID   message handle

                    #       ARGS    user-supplied arguments:

                    #               There are no user supplied arguments

                    #

                    # Returns: tps disposition list:

                    #

                    #

                    proc kill_A04wNoAccount { 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

                       # Here we retrieve the data from our original message

                       set msg [msgget $mh]

                       # Now we need to determine our field and subcomponent seperatoprs

                       set field_sep [csubstr $msg 3 1] ;# HL7 field separator    

                       set sub_sep [csubstr $msg 4 1] ;# HL7 subfield separator    

                       set disposition “KILL”

                         

                       # Here we spilt the original message into a list of the segments contained within

                       set segmentList [split $msg r]

                       foreach segment $segmentList {

                    if {[cequal [crange $segment 0 2] MSH]} {

                    set fieldList [split $segment $field_sep]

                    set msh_9 [lindex $fieldList 8]

                    set msh_9_list [split $msh_9 $sub_sep]        

                        set msh_9_1 [lindex $msh_9_list 0]

                        set msh_9_2 [lindex $msh_9_list 1]

                    if {![cequal $msh_9_2 “A04”]} {

                            set disposition “CONTINUE”

                        }

                    } elseif {[cequal [crange $segment 0 2] PID]} {

                    set fieldList [split $segment $field_sep]

                    set pid_18 [lindex $fieldList 18]

                    if {[string length $pid_18] == 0 }  {

                    set disposition “CONTINUE”

                    } elseif {[cequal $pid_18 “”””]} {

                    set disposition “KILL”

                    }

                    }    

                       }

                       lappend dispList “$disposition $mh”            

                    return $dispList                

                      }

                      time {

                    # Timer-based processing

                    # N.B.: there may or may not be a MSGID key in args

                      }

                      shutdown {

                    echo “kill_A04wNoAccount is shutting down”

                      }

                      default {

                    error “Unknown mode ‘$mode’ in kill_A04wNoAccount”

                      }

                    }

                    return $dispList

                    }

                    ****************************************************

                    Not sure if I should be specific to the PID-18.1 or if PID-18 is sufficient…..

                  • #67441
                    Robert Milfajt
                    Participant

                      Quote:

                      C’mon Bob, don’t tell him to use the cequal command.  I’m trying to break those habits  

                      Somethin about old dogs and new tricks, but in my advancing years, I cannot seem to remember the saying…    ðŸ˜€

                      WOOF!

                      Robert Milfajt
                      Northwestern Medicine
                      Chicago, IL

                    • #67442
                      David Barr
                      Participant

                        Robert Milfajt wrote:

                        Quote:

                        C’mon Bob, don’t tell him to use the cequal command.

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