Split a msg from a buffer

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Split a msg from a buffer

  • Creator
    Topic
  • #50979
    Bala Pisupati
    Participant

      I have a situation where I open a file in tcl proc and I am getting the value line by line. Since I am getting the whole file into a variable I have all the hl7 records in the variable. I have to split the records in the variable and display only the record with requested PID.

      I have file with about 10 oru messages

      I opened the file in tcl using open (like in file open)

      I read the file line by line into a buffer

      set data [exec cat

    Viewing 4 reply threads
    • Author
      Replies
      • #68274
        Jerry Tilsley
        Participant

          First I split $data based on newlines:

          set lines [split $data n]

          then I search  and retrieve the list location of all the lines for ones that start with:

          set msh_loc [lsearch -regexp -all $lines “^MSH”]

          then you can loop through all lines in $data and build them into a list of messages:

          set msg “”

          set msgs “”

          foreach seg $segs {

          if {[string match [string range $seg 0 2] “MSH”] > 0} {

          if {$msg != “”} {

          lappend msgs $msg

          }

          set msg “$segr”

          } else {

          set msg “$msg$segr”

          }

          }

          now you have a list of msgs in $msgs that you can perform other tcl code on.

        • #68275
          Keith McLeod
          Participant

            If the file has messages that are newline delimited try using:

            for_file ./ {

            # This reads one line at a time into the for_file loop into variable

            #Do stuff here….

            }

            Example:

            for_file msg ./oru_file.txt {

            # This reads one line at a time into the for_file loop into variable

            #Do stuff here….

            set segments [split $msg r]

            other code here…..

            }

          • #68276
            David Barr
            Participant

              Assuming the file doesn’t contain newlines and only contains carriage returns between segments, this should work:

              Code:

              set data [read_file test.old.msg]
              set match 0
              foreach segment [split $data r] {
               if { [regexp {^PID} $segment] } {
                 set fields [split $segment [string range $segment 3 3]]
                 set match [expr [lindex $segment 3] eq “mynumber”]
               } elseif { [regexp {^MSH} $segment] } {
                 if { $match } {
                   puts $msg
                 }
                 set match 0
                 set msg “”
               }
               append msg “$segmentn”
              }
              if { $match } {
               puts $msg
              }

            • #68277
              Bala Pisupati
              Participant

                Thanks for all your replies, I will try and let you know how it goes.

              • #68278
                Bala Pisupati
                Participant

                  Thanks Jerry Keith David for your inputs.

                  I went with David’s suggestion because that is exactly what I was looking for and it is working good. Thanks all–

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