x12 -tcl code: writing to output files

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf x12 -tcl code: writing to output files

  • Creator
    Topic
  • #48850
    Femina Jaffer
    Participant

      Hello Clovertechies,

      I would really appreciate it if someone would look at my code and tell me what I am doing wrong.  I have really exhausted myself in figuring this out for the last few days.

      The code is doing what it is supposed to except when it reads multiple files.  For example, I may have several .DAT files in a directory path:  The code is supposed to read each .DAT file, process the file and then write the output file to a .dat file.  Well, it works alright if there is one file in the directory.  However, if there are multiple .DAT files to process, it will read all of them but write out the output of the last file to all the files.  For example, if there are two files, the output of the second file is written to both files.  Thus, two .dat files with the correct filename but same output.

      Thank you so much for your help.

      FJ

    Viewing 6 reply threads
    • Author
      Replies
      • #59908
        Nora Katz
        Participant

          Do you need to close the current file you are writing to at some point and open the next file?

        • #59909
          Femina Jaffer
          Participant

            Thanks Nora,

            By adding that to the code, I am still having the same probem.  It seems that the outMsg is not getting cleared, because after it writes the first file, it opens the second file and write the same message into the second file.

            fj

          • #59910
            Nora Katz
            Participant

              if {$count != -1}   This statement seems to be true all the time?  When is count ever -1?

              You originally set count to 0

            • #59911
              Steve Carter
              Participant

                I found a few things.

                1.  There are a few lines of code that aren’t used anywhere.  I removed those.

                2.  outMsg wasn’t getting reset to “” after each file

                3.  outputfile wasn’t being closed.

                4.  count wasn’t being reset after each file.

                I also changed the count line to ‘count > 0’.  I’m not sure what you’re trying to write out to the files.  Based on how your code is setup, the only time you’ll get an output file is when you run across a file that contains a CLP segment.  Otherwise your counter will never be incremented and it won’t write your output.

                The attached code may still not work  since I’m not exactly sure what you’re trying to do.  However, it may help you figure it out.

                Steve

              • #59912
                Femina Jaffer
                Participant

                  Thank you Steve and Nora, I will consider all you have said and view the modified code, that may be needed for implementation.  Thank you so much for your assistance.

                  Steve,

                  I am actually reading claim files in an x12 format.  If the CLP segment has a “Z” prefix in the first field, I need to remove it.  I then write out the file into another file after stripping the “Z”.  If the CLP segment does NOT have a “Z” as a prefix in the first file, I still write it out to a file.

                  The directory may have multiple .DAT files, and I have to read each of the files and write out the content.

                  Thank you.

                  fj

                • #59913
                  Steve Carter
                  Participant

                    OK.

                  • #59914
                    Femina Jaffer
                    Participant

                      Well, everyone I finally got it!  I had to actually READ the file first, what a concept!

                      Thank you again, Nora and Steve for your help.  It was much appreciated.  Here is the fixed code:

                             run {

                                 # ‘run’ mode always has a MSGID; fetch and process it

                                 keylget args MSGID mh

                                 set msg [msgget $mh]

                                 # set dir /hci/FTP/EOB/OUT

                                 # set dir /hci/root3.8.1P/

                                 set dir /hci/root3.8.1P/testressite/

                                 cd $dir

                                 set count 0

                                 set segCount 0

                                 set segID “”

                                 set splitMsg [split $msg ~]

                                 set filename “”

                                 set outputfile “”

                                 set filenameExt “”

                                 set Ext “”

                                 set fname “”

                                 # set segmentList [split $msg ~]

                                   foreach filename [glob -nocomplain *.DAT] {

                                     set outMsg “”

                                     set count 0

                                     set inFile [open $filename r]

                                     set outputfile [file rootname [file tail $filename]]

                                     set segmentList [ split [read $inFile] ~]

                                     foreach seg $segmentList {

                                         set CLPseg [lindex $seg 0]

                                         set CLPID [string range [lindex $seg 0] 0 2]

                                         if {$CLPID !=”CLP”} {

                                             lappend outMsg $seg

                                         } else {

                                             set z_fld [lindex [split $seg *] 1]

                                             set z_ind [string range $z_fld 0 0]

                                             if { $z_ind !=”Z” } {

                                                 lappend outMsg $seg

                                             } else {

                                                 regsub “Z” $CLPseg “0-” nseg

                                                 set seg $nseg

                                                 lappend outMsg $seg

                                             }

                                             incr count

                      Thank you again.

                      fsj

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