Proc: Write to File Based on Value of Given Field

Clovertech Forums Read Only Archives Cloverleaf Tcl Library Proc: Write to File Based on Value of Given Field

  • Creator
    Topic
  • #55751
    Brian Mertens
    Participant

      Hi Everyone –

               I’m trying to write out a proc to dynamically write to a given file, that file being dependent on the value of a given field (in this case, RXA-11.1). Essentially, the logic would be: IF RXA-11.1 eq a||b||c|| then open file 1; ELSE open file 2.

      the code that I’ve written so far will parse out the message and isolate the value of RXA-11.1, but I’m having trouble scripting out the conditional logic for which file to open. Here’s the existing code from the run section:

      keylget args MSGID mh

      set msg [msgget $mh]

      set fieldSep [string index $msg 3]

      set subSep [string index $msg 4]

      set segList [split $msg r]

      set segment [lsearch -inline -regexp $segList ^RXA]

      set fieldList [split $segment $fieldSep]

      set rxa_11 [lindex $fieldList 11]

      set subFieldList [split $rxa_11 $subSep]

      set rxa_11_1 [lindex $subFieldList 0]

      lappend dispList “CONTINUE $mh”

      any input would be appreciated, thanks in advance!

      -Brian

    Viewing 4 reply threads
    • Author
      Replies
      • #86394
        Charlie Bursell
        Participant

          Things to consider:

          Will the value always be in lower case or does case matter at all

          Will you write to a new file each time or will you append to file?

          What file naming convention will you use?

          Most important, what will be written to the file?

          Several ways to implement the logic:

          if {$rxa_11 eq “a” || $rxa_11 eq “b” || $rxa_11 eq “c”} (

                  open file 1

          } else {

                  open file 2

          }

          switch -exact $rxa_11 }

               a –

               b –

               c    {open file 1}

               default {open file 2)

          }

          Many other ways.  Depends on who is doing it and which way they choose.  Most are as good as another.

        • #86395
          Brian Mertens
          Participant

            Thanks Charlie, apologies for the sparse details on the original post. So, I went with a switch statement to cover the logic since there were a handful of potential options. At this point I’m trying to create or append a newline terminated file, which is created if it doesn’t already exist. I’m thinking that the code for each scenario in the switch statement would first check to see if the file exists. If not, create it (newline, append). After the check/creation, open, write the message to the to file, and close. I’m just not sure how to actually code this. If you have any pointers, it’s much appreciated. Thanks!

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

            keylget args MSGID mh

                       set msg [msgget $mh]

                       set fieldSep [string index $msg 3]

                       set subSep [string index $msg 4]

                       set segList [split $msg r]

                       set segment [lsearch -inline -regexp $segList ^RXA]

                       set fieldList [split $segment $fieldSep]

                       set fieldList [lreplace $fieldList 6 6 1]

                       set segment [join $fieldList $fieldSep]

                       set position [lsearch -regexp $segList ^RXA]

                       set segList [lreplace $segList $position $position $segment]

                       set rxa_11 [lindex $fieldList 11]

                       set subFieldList [split $rxa_11 $subSep]

                       set rxa_11_1 [lindex $subFieldList 0]

                       set dest_Dir $HciSiteDir/dir/subdir/out

                       set msg [join $segList r]

                       msgset $mh $msg

                       switch -exact — $rxa_11_1 {

                           facility1 {

                               set fileId [open $dest_Dir/facility1 RDWR 0666]

                           }

                           facility1 {

                               set fileId [open $dest_Dir/facility1 RDWR 0666]

                           }

                           facility2 {

                               set fileId [open $dest_Dir/facility2 RDWR -0666]

                           }

                           facility2 {

                               set fileId [open $dest_Dir/facility2 RDWR 0666]

                           }

                           facility3 {

                               set fileId [open $dest_Dir/facility3 RDWR 0666]

                           }

                           facility3 {

                               set fileId [open $dest_Dir/facility3 RDWR 0666]

                           }

                           facility4 {

                               set fileId [open $dest_Dir/facility4 RDWR 0666]

                           }

                           facility4 {

                               set fileId [open $dest_Dir/facility4 RDWR 0666]

                           }

                           facility5 {

                               set fileId [open $dest_Dir/facility5 RDWR 0666]

                           }

                           facility5 {

                               set fileId [open $dest_Dir/facility5 RDWR 0666]

                           }

                           facility6 {

                               set fileId [open $dest_Dir/facility6 RDWR 0666]

                           }

                           facility6 {

                               set fileId [open $dest_Dir/facility6 RDWR 0666]

                           }

                           facility7 {

                               set fileId [open $dest_Dir/facility7 RDWR 0666]

                           }

                           facility7 {

                               set fileId [open $dest_Dir/facility7 RDWR 0666]

                           }

                           default {

                               set fileId [open $dest_Dir/DefaultFacility RDWR 0666]

                           }

                           

                       }

                       echo $fileId

                       

                       lappend dispList “CONTINUE $mh”

                   }

          • #86396
            Charlie Bursell
            Participant

              Unless I am not understanding the problem, it seems it could be greatly simplified:

                 run {

                     keylget args MSGID mh

                     set msg [msgget $mh]

                     set fieldSep [string index $msg 3]

                     set subSep [string index $msg 4]

                     set segList [split $msg r]

                     # You need the position of the RXA so use it in both cases

                     set loc [lsearch -regexp $segList {^RXA}]

                     set RXA [split [lindex $segList $loc] $fieldSep]

                     set RXA [lreplace $fieldList 6 6 1]

                     set segList [lreplace $segList $loc $loc [join $RXA $fieldSep]]

                     # Will the value of RXA.11.1 always be lower case?

                     # You might add something like:

                     # [string tolower [lindex [split [lindex $RXA 11] $subSep] 0]]

                     set rxa_11_1 [lindex [split [lindex $RXA 11] $subSep] 0]

                     # Let Tcl build the path with correct separators

                     # Also add the proper end path here instead of in switch

                     set dest_Dir [file join $::HciSiteDir dir subdir out $rxa_11_1]

                     # Do this here or at the end, don’t matter

                     set msg [join $segList r]

                     msgset $mh $msg

                     # To open simply open in append mode “a”

                     # If the file does not exist, append creates a new empty file.

                     # If it exist will append to the file

                     # If these are HL7 messages you may want to open in binary “b” mode

                     # Default file permissions are: 0666

                     # Why is switch needed?  You have already set directory path above

                     # Simply

                     set fd [open $dest_Dir a]

                     # Or if HL7

                     # set fd $dest_Dir ab]

                     puts $fd

                     close $fd

                     return “{CONTINUE $mh}”

                 }

            • #86397
              Brian Mertens
              Participant

                Couple of take-aways from the above post:

                -lines 9-11 : cleaned up the RXA segment locator. much better, I appreciate that.

                lines 17-20 : cleaned up RXA-11.1 delimiter by nesting commands to split and index RXA. much better, also appreciated.

                -lines 36-40 : this is where the switch comes in. Unfortunately, as I see it, the switch is required. Ultimately, the file that is written to will be decided by the value of RXA-11.1, but the filename will not always match that value. essentially, there’s 8 possible files. 7 of them are associated with 14 potential values (2x potential values to each file). The 8th file is a catch all, which should be written to if the value of RXA-11.1 is not one of the 14 aforementioned values. Because there’s so many potential values that could be received in that field, a switch statement seems most efficient for handling it.

                So, I updated my code, and it now points to the right file path based on the value of RXA-11.1, but I’m having trouble with opening, writing to file, and then closing. I’m sure its a one-off that I’ve been missing, but I can’t seem to get the expected behavior *note, I haven’t made some of the cosmetic/clean-up changes to the code yet, I want to get the file write commands working before I go back to clean up:

                run {

              • #86398
                Brian Mertens
                Participant

                  I was able to finally resolve this with a little tinkering, thanks!

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