TCL script for iterating through a field.

Clovertech Forums Read Only Archives Cloverleaf Tcl Library TCL script for iterating through a field.

  • Creator
    Topic
  • #54437
    Robert Denny
    Participant

      We have some procs that will pull the value of a subfield and run it through a table. If the Value meets a criteria we then send the message, if not we kill it.

      Now we have certain fields that will more than one value to compare to the table and I could use the help from an advanced TCL script write to suggest ways to iterate through the field and compare the first value of each set and compare to the table.

      This is for fields in which there are more than one providers listed. Such as OBR28.

      How do I pull the first subfield from each set and compare to the table?

      Here is an example of the current script. I am wondering if I can modify it for the part in which we pull data from OBR28?

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

      # Name: abchouse_filter_lab

      # Purpose: Restrict messages sent to those in the abc_prov table

      #

    Viewing 9 reply threads
    • Author
      Replies
      • #81466
        Michael Hertel
        Participant

          Try this:

          ######################################################################
          # Name: abchouse_filter_lab
          # Purpose: Restrict messages sent to those in the abc_prov table
          #
          [code]######################################################################
          # Name: abchouse_filter_lab
          # Purpose: Restrict messages sent to those in the abc_prov table
          #

        • #81467
          Robert Denny
          Participant

            Thank you for this example.

            I will test it and see how it works. So is this only grabbing the first field after the ~?

          • #81468
            Michael Hertel
            Participant

              You can think of it that way, yes.

              Technically you’ll see:

              123^Apple^Alison~234^Bear^Betty~345^Chatty^Cathy

              Which will be broken down to:

              123^Apple^Alison

              234^Bear^Betty

              345^Chatty^Cathy

              Then each of those will be evaluated on:

              123

              234

              345

              The first one that makes a match in your table will cause the proc to stop and continue the message. If it goes through all of them and doesn’t find a match it will kill the message.

            • #81469
              Michael Hertel
              Participant

                Was wondering how/if this code worked for you.

              • #81470
                Kevin Crist
                Participant

                  If we don’t use the getHL7Field how else would we make this work? I am needing to do the same thing and not having very good luck with the OBR.28 looping?

                  Thanks for any help.

                • #81471
                  Charlie Bursell
                  Participant

                    As some may know I am not a huge fab of the so called HL7 library.  I find really inefficient when there are core commands to do the same.  When you call those routines the same code is executed over and over.

                    I do really like the algorithm used.  Good implementation of available Tcl commands.

                    I have a tendency to get a bit loose with my tying so validate for correct spelling   😀

                       # No hard coded separators

                       set fldSep [string index $msg 3]

                       set subSep [string index $msg 4]

                       set iterSep [string index $msg 5]

                       # List of segments

                       set segList [split $msg r]

                       # Get segment fields

                       set segmentPV1 [split [lsearch -inline -regexp $segList {^PV1}] $fldSep]

                       set segmentOBR [split [lsearch -inline -regexp $segList {^OBR}] $fldSep]

                       set admitValuelist [split [lindex $segmentPV1 8] ~]

                       set pcpValuelist [split [lindex $segmentPV1 9] ~]

                       set referValuelist [split [lindex $segmentPV1 17] ~]

                       set orderValuelist [split [lindex $segmentOBR 16] ~]

                       set ccedValuelist [split [lindex $segmentOBR 28] ~]

                       # change hard coded separators

                       [tbllookup $table [lindex [split $doctor $subSep 0]] 1]}

                       # This statement needs to be validated so the proc does not blow up

                       keylget args ARGS.DOCTABLE table

                       # Try something like:

                       set table “”; keylget args ARGS.DOCTABLE table

                       if {[catch {tbllookup $table xxxx} err]} {

                           echo nnabchouse_filter_lab: Table Error: $errn

                           echo No action taken = messages remain in recovery DBnn

                           # Return empty string will leave messages in recovery database

                           # you could also send e-mail or whatever here

                           return “”

                       }

                  • #81472
                    Kevin Crist
                    Participant

                      I have this proc below. It goes through on the OBR.28 and fines the first field of each repetition. But when it sets the flag it over writes it with the next iteration. I thought putting in the “break” would stop that but it doesn’t seem to. How do you get it to stop looping if it finds what you are looking for?

                      Code:

                      #########################################################################################
                      #########################################################################################
                      #########################################################################################
                      # Name: poi_obr16_obr28_filtering
                      # Purpose: This proc will KILL the mesages based on if the the Dr. Nmemonic is in the
                      # OBR.16 field and the OBR.28 field. This will use the poi_allscripts_filter.tbl
                      #
                      # For use of arguments just put in one of the following 3 letter codes for the practices:
                      # AMW  for Arthritis Medicine & Wellness
                      # EFP  for Elizabeth Family Practice
                      # SEL  for Southeast Lung
                      # GCS  for Gastroenterrology Consultants
                      # OPT  for Optim Orthopedics
                      #
                      # UPoC type: tps
                      # Args: tps keyedlist containing the following keys:
                      #       MODE    run mode (”start”, “run” or “time”)
                      #       MSGID   message handle
                      #       ARGS    user-supplied arguments:
                      #
                      #
                      #
                      #
                      #
                      # Created: Kevin Crist 03/25/2016
                      #
                      #
                      #########################################################################################
                      #########################################################################################
                      #########################################################################################

                      proc poi_obr16_obr28_filtering { args } {

                      #########################################################################################
                      # Get the Connection/Proc Name for Error/Debug Messages
                      #########################################################################################

                      global HciConnName
                      set myname “$HciConnName/[lindex [info level 1] 0]”
                      set nowis [clock format [clock scan now] -format “%D – %T”]

                      #########################################################################################
                      # Initialize Variables Used
                      #########################################################################################

                      set dispList “” ;# Disposition List Returned to Engine

                      set fldSep “” ;# Field Seperator – get from MSH
                      set subSep “” ;# SubField Seperator – get from MSH
                      set repSep “” ;# Repeating Field Seperator – get from MSH

                      set OBRpos -1 ;# Position of OBR Segment in msg

                      set OBRList [list] ;# OBR Segment Fields in List
                         set OBRDrList [list] ;# OBR Subfields in  a list
                      set practice “”         ;# Sending Application – get from ARGS
                         set OBRDrCode 16 ;# Dr Service Code
                      set OBRCopyTo 28 ;# Dr Copy to field
                         set table “poi_allscripts_filter”    ;# Table with Doctor Codes

                       
                      #########################################################################################
                      # Switch based on what mode the engine was in when it called the procedure
                      #########################################################################################

                      keylget args MODE mode             ;# The mode the engine called from
                      keylget args ARGS practice ;# Argument for which practice to route to
                      set practice [string toupper $practice] ;# Force to uppercase

                       switch -exact — $mode {

                        start { }

                         run {
                          set mh [keylget args MSGID] ;# Get message handle from args

                           set msg [msgget $mh] ;# Get a copy of the message
                         
                           set fldSep [string index $msg 3] ;# Field Seperator
                           set subSep [string index $msg 4] ;# Sub-Field Seperator
                           set repSep [string index $msg 5] ;# Repeating Field Seperator

                      set segList [split $msg r] ;# Split message into segList

                      set OBRpos [lsearch -regexp $segList {^OBR}]
                      #########################################################################################
                      #Finds the dr code in OBR.16
                      #########################################################################################

                       set OBRList [split [lindex $segList $OBRpos] $fldSep]

                       
                      # set drcode [lindex [split [lindex $OBRList $OBRDrCode] $subSep] 0]

                      set OBRDrList [split [lindex $OBRList $OBRCopyTo] $repSep]

                      foreach Dr $OBRDrList {
                               set DrNumber [lindex [split $Dr $subSep] 0]

                                 foreach value $DrNumber {
                                   set value [tbllookup $table $DrNumber]

                                      foreach value $value {  
                              if {[string equal $value $practice]} {
                            set flag “Y”

                        break
                      } else {
                             set flag “N”

                      }
                      }
                       }
                      }
                      #########################################################################################
                      # Check the Dr.Code with the poi_amw_filter.tbl and see if it is a match. If there is a match
                      # then the message would be killed otherwise it will continue.
                      #########################################################################################

                      if {[string equal $flag “Y”]} {
                      lappend dispList “CONTINUE $mh”
                      } else {
                      lappend dispList “KILL $mh”
                      }

                       

                      }

                         time { }
                         shutdown { }
                      } ;# End Switch

                      return $dispList

                      } ;# End Proc

                    • #81473
                      James Cobane
                      Participant

                        I would simply initialize the flag to N prior to starting your looping, then flip it to Y when it hits the condition in the loop; the ‘else’ within the loop is what is tripping you up.  You could also simply set the disposition when the condition is true and then return at that point.

                        Jim Cobane

                        Henry Ford Health

                      • #81474
                        Kevin Crist
                        Participant

                          ok, cool. that seemed to work.

                          Is there a better way to do the foreach on the OBR.28? I have 3 and even though it is working I don’t feel like that is very efficient.

                        • #81475
                          David Barr
                          Participant

                            Kevin Crist wrote:

                            Is there a better way to do the foreach on the OBR.28? I have 3 and even though it is working I don’t feel like that is very efficient.

                            I can’t quite follow what you’re doing. This part looks wrong:

                            Code:

                            set value [tbllookup $table $DrNumber]

                                            foreach value $value {  

                            What’s does the value you get back from tbllookup look like? Is it a TCL list? If not, then you can’t use it for a foreach loop. And why is the name of the variable set in your loop the same as the list of values?

                        Viewing 9 reply threads
                        • The forum ‘Tcl Library’ is closed to new topics and replies.