Tcl Help

  • Creator
    Topic
  • #51682
    Jared Miller
    Participant

      I am attempting to use this tcl code to filter messages that are of PAO patient type (PV1-18) and of only a certain facility code (MSH 3.4).  Please review my code and offer suggestions for improvement for this code or if I’m going about this all wrong then I would appreciate some guidance.  Thanks in advance!

      {

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

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

         set segmentList [split $msg r]

         # Now we iterate over each segment in our list.

        foreach segment $segmentList {

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

      set fieldList [split $segment $field_sep]

      set pid_3 [lindex $fieldList 3]

                        # split PID-3 on the sub-seperator

                              set pid_3_list [split $pid_3 $sub_sep]

                        #  get the first element in PID-3

                           set pid_3_4 [lindex $pid_3_list 3]

                 

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

      set fieldList1 [split $segment $field_sep]

      set pv1_18 [lindex $fieldList1 18]

                         # split PV1-18 on the sub-seperator

                              set pv1_18_list [split $pv1_18 $sub_sep]

                          #  get the first element in PV1-18

                            set pv1_18_1 [lindex $pv1_18_list 0]

      if {[cequal $pv1_18_1 “PAO”] && [cequal $pid_3_4 “W”]}{

      lappend dispList “KILL $mh”

      return $dispList

      } else {

      lappend dispList “CONTINUE $mh”

      return $dispList

      }

      }  

      }

      }

             }

    Viewing 5 reply threads
    • Author
      Replies
      • #71265
        David Barr
        Participant

          The code looks OK to me, but you asked for suggestions, so here are my opinions.

          1. when posting code to Clovertech, wrap it in

          Code:

          tags so that the formatting is retained.

          2. Use an HL7 parsing library instead of all this split, foreach, lindex nonsense.

        • #71266
          Scott Folley
          Participant

            Well, here is my take on it:

            Code:


            # ‘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 [ string range $msg 3 3] ;# HL7 field separator
            set sub_sep [string range $msg 4 4] ;# HL7 subfield separator
            # Here we spilt the original message into a list of the segments contained within
            set segmentList [split $msg r]

            # Seed the variables so that they exist at the proc scope
            set pid_3_4 “”
            set pv1_18_1 “”

            # Now we iterate over each segment in our list.
            foreach segment $segmentList {
               set fieldList [ split $segment $field_sep ]
               set segId [ lindex $fieldList 0 ]
               switch -exact — $segId {
                   PID {
                       set pid_3 [lindex $fieldList 3]
                       # split PID-3 on the sub-seperator
                       set pid_3_list [split $pid_3 $sub_sep]
                       #  get the first element in PID-3
                       set pid_3_4 [lindex $pid_3_list 3]
                   }
                   PV1 {
                       set pv1_18 [lindex $fieldList 18]
                       # split PV1-18 on the sub-seperator
                       set pv1_18_list [split $pv1_18 $sub_sep]
                       #  get the first element in PV1-18
                       set pv1_18_1 [lindex $pv1_18_list 0]
                   }
               }
            }
            if { $pv1_18_1 == “PAO” && $pid_3_4 == “W” }{
               lappend dispList “KILL $mh”
            } else {
               lappend dispList “CONTINUE $mh”
            }

            I have to admit that I did not actually test this code but I am relatively confident that it is in the right ballpark.

            I agree with David that the cequals and the other procs of that ilk are a poor choice because they can have performance issues.

            When it comes to code libraries that really depends on how you maintain your code.  There will always be an arguable point when it comes to reusability because it is a double edged sword.  The same thing that allows you to fix everything in one place also allows you to break everything in one place.

            Now you have my $.02

          • #71267
            Jared Miller
            Participant

              Thanks for the input…I have been trying to implement the code provided by Scott, but keep getting this error below.  I have tried several different things, but can’t get past this error:

              Code:

              [sms :sms :ERR /0:    neobrowse] Tcl error:
              msgId = message0
              proc = ‘kill_pao’
              args = ”
              result = ‘extra characters after close-brace’
              errorInfo: ‘
              extra characters after close-brace
                 while compiling
              “if { $pv1_18_1 == “PAO” && $pid_3_4 == “W” ”
                 (”run” arm line 40)
                 invoked from within
              “switch -exact — $mode {
                     start {
                         # Perform special init functions
                 # N.B.: there may or may not be a MSGID key in args
                    …”
                 (procedure “kill_pao” line 6)
                 invoked from within
              “kill_pao {MSGID message0} {CONTEXT sms_ob_data} {ARGS {}} {MODE run} {VERSION 3.0}”‘

            • #71268
              David Barr
              Participant

                I think you need a space between the }{ characters. Also, “==” is for numeric comparisons.  You should be using “eq” for string comparisons.

              • #71269
                Jared Miller
                Participant

                  Thanks, I have it working now.

                • #71270
                  Scott Folley
                  Participant

                    Ah yes, David, good catch.  I hope it is working well now, I had not actually had an opportunity to try it out.  It was more of an example of the coding style that I use to handle this type of problem.

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