Reading a file

Clovertech Forums Read Only Archives Cloverleaf Tcl Library Reading a file

  • Creator
    Topic
  • #54530
    Mike Campbell
    Participant

      I’ve successfully been able to create and write a file out using tcl.

      Now, in another xlate, I need to read the file and retrieve a specific value.

      The table consists of the Message ID from the MSH segment and a Medical Record Number.

      I’m doing the read using a foreach loop.  It is reading all of the records in the file, but I’m not able to select the one I need.  Do I need to set it up as an array?  I set up a counter that does increment as the foreach is executed.  The $message_id is passed out of the Xlate

      CL 6.0.2.  AIX system.

      [Just s snippet of the tcl code.  All variables are defined.]

        foreach line [ split $msg n ] {

      echo “Data is: ” $msg

      incr indx

      echo “Counter is: ” $indx

      set file_msg_id [ string range $msg 0 19 ]

      set mrn_found_id [ string range $msg 21 27 ]

         if { $file_msg_id == $message_id } {

            set RESULTS $mrn_found_id

         echo “Results to send back is:” $RESULTS

          }

         }

    Viewing 4 reply threads
    • Author
      Replies
      • #81857
        James Cobane
        Participant

          Mike,

          You may want to consider utilizing the SQLite functionality for this type of store/retrieval.  Check the link below for some examples.

          https://usspvlclovertch2.infor.com/viewtopic.php?t=6686&start=0&postdays=0&postorder=asc&highlight=

          Jim Cobane

          Henry Ford Health

        • #81858
          Mike Campbell
          Participant

            Thanks James.  Our management would prefer we don’t use the SQLite option.  So I’ll try to figure out the array if that is the only other way.

          • #81859
            James Cobane
            Participant

              Mike,

              Just curious as to why your management is opposed to the SQLite option.  It’s functionality built into Cloverleaf / Tcl and is probably more efficient than using straight file-based lookups.

              Jim Cobane

              Henry Ford Health

            • #81860
              Charlie Bursell
              Participant

                Mike

                I, like Jim, am perplexed as to why your management would not want you to use the best tool for the job.

                With that said, if you must do the file there are two issues.  How often does the contents of the file change?  Do you need to read the whole file everytime?  Is the format of the file consistent?

                You do not need to loop through every line or read the entire file every time.

                Here is what I woud do (making some assumptions)

                I would store the contents of the file and last mod time of the file in global variables – in Xlate, by definition all variables are global.  

                If the variables do not exist it is the first time, so read the file into the proper variable (use read_file) and the last mod time into the proper variable.  If the variables exist, get the last mod time of the file.  If it is different, read the file and update the last mod time.  Else leave as is.

                Assuming the file you created is like:  (FIELD FIELD)

                MSGID MRN

                MSGID MRN

                Use a regexp

                set mrn “”; regexp -line — “^$message_id (.*?)$” $file {} mrn

                If mrn is empty the ID was not found.  Do something.

                You might want to make sure the file exists before any of this and take some sort of action if not –  if {![file exists ]} {Do something}

                In regexp:

                -line

                Enables newline-sensitive matching. By default, newline is a completely ordinary character with no special meaning. With this flag,

              • #81861
                Mike Campbell
                Participant

                  I’ve convinced the folks to let me try the sqlite approach.  Working on that now.  Thanks Charlie for the file option if needed.

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