TCL Code

  • Creator
    Topic
  • #49024
    Ricci Graham
    Participant

      I am writing some TCL code that should be really simple, but I am hung up on something. I am not getting the result I am expecting from the “split” command.

      I am trying to split a file into segments so I can strip out all the HDR segments except the first one. This is being done after the file leaves the engine because I am building a batch by stopping the thread at 11:59 p.m. and moving the file to another location and then running this code against it. The file has a CRLF after each segment per the vendor specs. Can anyone give some suggestions on what I am doing wrong.

      Here is my code:

      set filelist

        ]

        set filenum [llength $filelist]

        if {$filenum >0 } {

         set filename [lindex $filelist 0]

         set fileid [open $filename r]

         set newfile

          ]

           set segments [split $newfile nr]

          Here is my result:

          {HDR|300|IMF|01172007|300 MM HD BIMAGE|1.0|01172007|01172007 {PAT|0000000|DOE|JANE|A||06171978|FEMALE|11600 SOMEWHERE RD||ANYWHERE|AR|999990000|(000)555-5555||O|S|576081061} {VIS|0000000|OUTPATIENT|9999|JOHN D. SMITH|||4|300|07112006|} {GUA|DOE|JANE|A||06171978|FEMALE|11600 SOMEWHERE RD||ANYWHERE|AR|999990000|(000)555-5555||0|SELF|5987979797979||01|SOMEPLACE TO WORK|||ANYWHERE|AR|9999900000|00000000000|0} {INS|1|00217|CIGNA|POB|999999||CHATTANOOGATN374227223|(800)555-5555|||} }

          Why am I getting the back-slashes and why dosn’t the first segment have a closing brace? It appears to close at the end, making it one big segment??

          I have tried varying versions of nr (i.e. “nr”, n, r, “rn”) and the only thing I get different is if I use the r or “r” I don’t get the segments broken apart, it is just one big segment with two sets of braces around it.

          Any suggestions or help would be appreciated.

          Thanks,

          Ricci Graham

          501-202-4202

        Viewing 18 reply threads
        • Author
          Replies
          • #60481
            Steve Carter
            Participant

              Try this:

              if {$filenum >0 } {

              set filename [lindex $filelist 0]

              set fileid [open $filename r]

              set newfile [read $fileid]

              regsub -all rn $newfile r newfile

              set segments [split $newfile r]

            • #60482
              Ricci Graham
              Participant

                Steve,

                Thanks for the suggestion. I tried it and it still isn’t working. I am still getting the same result????

                Any other suggestions?

                Ricci

              • #60483
                Michael Hertel
                Participant

                  Steve’s code should have worked, but try this:

                  if {$filenum >0 } {

                  set filename [lindex $filelist 0]

                  set fileid [open $filename r]

                  set newfile [string map {rn r} [read $fileid]]

                  set segments [split $newfile r]

                • #60484
                  Ricci Graham
                  Participant

                    Michael,

                    This didn’t work either, it gives the following error:

                    bad option “map”: must be compare, first, index, last, length, match, range, tolower, toupper, trim, trimleft, trimright, wordend, or wordstart.

                    But none of this really should matter because when I read up on the “read” command, it says when it reads the file in it converts the file so all text lines are terminated with n. So I should be able to just use the n as the character to “split” my segments on, but it is still giving me the back-slashes and leaving the first segment as part of one big segment. I can’t figure this one out.

                    Any other ideas?

                    Thanks,

                    Ricci

                  • #60485
                    Nora Katz
                    Participant

                      enclose in quotes?  “rn”

                    • #60486
                      Ricci Graham
                      Participant

                        Nora,

                        I tried that and it didn’t work either. Basically, I can’t figure out why it is putting in the back-slashes and thinks the first segment is part of the entire segment???

                        Thanks,

                        Ricci

                      • #60487
                        Michael Hertel
                        Participant

                          Ricci Graham wrote:

                          but it is still giving me the back-slashes and leaving the first segment as part of one big segment. I can’t figure this one out.

                          The reason you are getting the backslaches is that your original code was “list happy”. Don’t use list in “set newfile

                            ]”.

                            You must be on a version of the engine that was pre “string map” so we’ll do it differently.

                            Let’s break down your code, so do this:

                            set filelist

                              ]

                              set filenum [llength $filelist]

                              if {$filenum >0 } {

                              set filename [lindex $filelist 0]

                              set fileid [open $filename r]

                              set newfile [read $fileid]

                              echo $newfile

                              #Notice, we didn’t use the list command.

                              #Does the output contain backslashes?

                              #Now, let’s get rid of the newlines incase they are in the message.

                              set newfile [join [split $newfile n] {}]

                              echo $newfile

                              #Should still not contain backslashes.

                              #Now split on carriage returns.

                              set segments [split $newfile r]

                              echo $segments

                              #You should have what you want

                              }

                            1. #60488
                              Ricci Graham
                              Participant

                                Michael,

                                You are SO smart, this worked to get the back-slashes out, but it still isn’t seeing the first segment as a “segment” (not wrapping it in braces) and that is the segment that I need to work with, so I need to be able to “get a hold” of that segment through out the file.

                                Any suggestions on that?

                                Thanks so much for you assistance so far!

                                Ricci

                              • #60489
                                Michael Hertel
                                Participant

                                  When you view a list, not all of them will be wrapped in curly braces.

                                  The braces are for your visual que. Normally lists are separated by spaces. If however, there is a space as part of the data, you will be shown the brackets.

                                  Anyhow, now add this:

                                  echo [lindex $segments 0]

                                  or

                                  set firstelement [lindex $segments 0]

                                  echo $firstelement

                                  That will be the first “real” element in the list.

                                  -mh

                                • #60490
                                  Ricci Graham
                                  Participant

                                    Michael,

                                    I finally have this part down and you’re right the first element does show up as an element even though it isn’t in braces.

                                    Now I need to figure out how to remove all the HDR segements except the first one. I have tried diffent things with the

                                    lsearch regexp $segments {^HDR}

                                    but it doesn’t take the -all switch in version 8.0 and that is what I am on.

                                    Do you have any ideas? Or does anyone have any ideas?

                                    Thanks,

                                    Ricci

                                  • #60491
                                    Michael Hertel
                                    Participant

                                      Yes, go through segments and build a new message.

                                      #Start a new list named newsegments with the first HDR segment

                                      lappend newsegments [lindex $segments 0]

                                      foreach seg $segments {

                                    • #60492
                                      Ricci Graham
                                      Participant

                                        Michael,

                                        When I tried this it raised an error. See below:

                                        invalid command name “crange”

                                          while executing

                                        “crange $seg 0 2”

                                           (“foreach” body line 2)

                                           invoked from within

                                        “foreach seg $segments {

                                                    if {![cequal [crange $seg 0 2] HDR]} {

                                                     }

                                                    }”

                                        Did I do something wrong?

                                      • #60493
                                        Michael Hertel
                                        Participant

                                          What version and platform of Cloverleaf are you on?

                                          Change [crange $seg 0 2] to [string range $seg 0 2]

                                          You did nothing wrong. I guess you are on an ancient version of Cloverleaf.

                                        • #60494
                                          Ricci Graham
                                          Participant

                                            I am on version 5.3 on Windows 2003.

                                            I changed it to the string range and now it is giving th same error on cequal. I tried just changing that to equal, but it still gave me the same error.

                                            I am writing this outiside the Cloverleaf engine because of having to build a batch, and the version of tcl that I am using for that is 8.0.

                                            Any ideas on the cequal or equal?

                                          • #60495
                                            Michael Hertel
                                            Participant

                                              Do this instead.

                                              if {![string equal [string range $seg 0 2] HDR]} {

                                              By the way, all this can be done in the engine as part of an interface.

                                              Cloverleaf can handle batch interfaces.

                                            • #60496
                                              Ricci Graham
                                              Participant

                                                Michael,

                                                I am building my batch by letting all the messages run through the engine and then stopping the outbound thread at 11:59 p.m. each night and moving the file to a different location and then restarting the thread.

                                                How would you do this inside the engine?

                                                This gave the following error:

                                                bad option “equal”: must be compare, first, index, last, length, match, range, tolower, toupper, trim, trimleft, trimright, wordend, or wordstart

                                                   while executing

                                                “string equal [string range $seg 0 2] HDR”

                                                   (“foreach” body line 2)

                                                   invoked from within

                                                “foreach seg $segments {

                                                            if {![string equal [string range $seg 0 2] HDR] } {

                                                               lappend $seg newsegments

                                                             }

                                                            }”

                                              • #60497
                                                Michael Hertel
                                                Participant

                                                  Can you use a newer version of tcl?

                                                  Do this:

                                                  if {[string range $seg 0 2] != “HDR”} {

                                                • #60498
                                                  Robert Gordon
                                                  Participant

                                                    You are looking at a list within a list.  You are getting the first record, but not the first segment inside the first record.  Try using lindex twice to get inseide the record.

                                                  • #60499
                                                    Ricci Graham
                                                    Participant

                                                      Robert,

                                                      Thanks for your reply. You were correct, that is what the problem was. I have the code figured out thanks to Michael Hertel and it is running well in my test environment.

                                                      Ricci

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