Removing CRLF from message using RegSub not working

Homepage Clovertech Forums Read Only Archives Cloverleaf Tcl Library Removing CRLF from message using RegSub not working

  • Creator
    Topic
  • #54251
    Weston Olmstead
    Participant

    I’m having a bit of an issue using a pre-processing script on an HL7 to remove carriage return line feed (0D0D) and replace it with just carriage return (0D).

    I have a message in the following format:

    Segment [0D0A]

    Segment [0D0A]

    Segment [0D0A]

    Segment [0D0A]

    that I’m trying to run a pre-processor against using regsub to get the following output:

    Segment [0D]

    Segment [0D]

    Segment [0D]

    Segment [0D]

    I’m using the following script, but for some reason when it translates the message it replaces the 0D0A with a space. This makes me think its identifying it correctly, but not performing the replace. I even tried changing the string that was supposed to be the text inserted to “TESTTEXT” just to see if I had indicated the special character incorrectly but this didn’t seem to help.

    I’m sure I’m overlooking something simple.

    proc tpsConvertCRLFToLF { args } {

       set argList [keylget args ARGS]    

       keylget args MODE mode               ;# Fetch mode

       switch -exact — $mode {

           run {

               set msgID [keylget args MSGID]    

               set msgText [msgget $msgID]        

               regsub -all  {rn} $msgText {r} msgText

               msgset $msgID [join $newmsg]

               return “{CONTINUE $msgID}”

                }  

           default {

           error “Unknown mode ‘$mode’ in tpsMessageFilter.”

           }

       }

    }

Viewing 3 reply threads
  • Author
    Replies
    • #80726
      James Cobane
      Participant

      Weston,

      I think the issue may be with your ‘msgset’ command; I don’t see where you are valuing the ‘newmsg’ variable and also the [join $newmsg] has no delimiter specified.

    • #80727
      bill bearden
      Participant

      Along with what James said, something else to look at…

      Try replacing the {r} with “r”.

      I struggle sometimes with the curly braces in Tcl. It seems like sometimes it means “take this string literally with no variable or command or escape sequence substitution” and sometimes it doesn’t.

      I tested with the following. It appears to me that, when using {r}, regsub is replacing the crlf with literally slash-r. The last value of b looks like it is just def but it is actually abcdef. In Windows, plain old just puts you back at the beginning of the current line so the def is overwriting the abc.

      Code:

      tcl>set a “abcrndefrn”
      abc
      def

      tcl>regsub — {rn} $a {r} b
      1
      tcl>echo $b
      abcrdef

      tcl>regsub -all {rn} $a {r} b
      2
      tcl>echo $b
      abcrdefr
      tcl>regsub -all {rn} $a “r” b
      2
      tcl>echo $b
      def
      tcl>echo [string length $b]
      8
      tcl>

    • #80728
      Charlie Bursell
      Participant

      Bill is correct.  The output side of a regsub is interpreted by Tcl.  The difference in quoting with curly braces and quotes is Tcl will not do substitution of the characters inside the braces.  For example {$hello} is simply $hello, not a variable  while $hello reference the varible.  Try this at the Tcl prompt

      tcl>set str {hellorThere}

      hellorThere

      tcl>set str “hellorThere”

      There

      With that said why use a regsub at all when string map is much more ergonomic and less arcane

      set msgText [string map “n {}” $msgText]

      or better

      set msgText [string map “n {}” [msgget $msgID] ]

    • #80729
      Weston Olmstead
      Participant

      Thank you all for the assistance. Its amazing how another set of eyes can catch a simple mistake!

      I also have a difficult time determining to use quotes or curley brackets, but that example was very helpful. Thank you also for the advice on string mapping, I’ll look into that!


      Edit: Forgot to include the solution that worked for me in case others find this thread. The regsub is currently working, but I’m looking into character maps as that seems the proper way to do this now.

      proc tpsConvertCRLFToLF { args } {

        set argList [keylget args ARGS]    

        keylget args MODE mode               ;# Fetch mode

        switch -exact — $mode {

            run {

                set msgID [keylget args MSGID]    

                set msgText [msgget $msgID]        

      regsub -all  {rn} $msgText “r” msgText

                msgset $msgID $msgText

                return “{CONTINUE $msgID}”

                 }  

            default {

            error “Unknown mode ‘$mode’ in tpsMessageFilter.”

            }

        }

      }

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

Forum Statistics

Registered Users
5,129
Forums
28
Topics
9,301
Replies
34,448
Topic Tags
288
Empty Topic Tags
10