TCL proc to unescape HL7 escape sequences

Clovertech Forums Read Only Archives Cloverleaf Tcl Library TCL proc to unescape HL7 escape sequences

  • Creator
    Topic
  • #47624
    Jason Alexander
    Participant

      David Barr from MedStar Health out in Maryland was kind enough to send me the following proc to unescape HL7 escape sequences in strings (like uuencoded data) where it is possible to find ambiguous strings that make it impossible to simply pattern match sequences one after the other.  You need to pass in the delimiter list as well as the string to process.

      ######################################################################

      # Name:         unescape_HL7

      # Purpose:      Remove HL7 escape sequences and replace them with

      #               the actual characters represented (including escaped

      #               hexadecimal values)

      # Args:         Two strings required

      #               input – the actual string to be unescaped

      #               delimiters – the delimiters as foudn in MSH-1 and MSH-2

      #

      # Comments:    

      proc unescape_HL7 { input delimiters } {

        set delim_f [string range $delimiters 0 0]

        set delim_s [string range $delimiters 1 1]

        set delim_r [string range $delimiters 2 2]

        set delim_e [string range $delimiters 3 3]

        set delim_t [string range $delimiters 4 4]

        set output “”

        while {[ string length $input ]} {

          set char [string range $input 0 0]

          set input [string range $input 1 end]

          #echo processing $char

          if { [string equal $char $delim_e] } {

              set esc “”

              set char [string range $input 0 0]

              set input [string range $input 1 end]

              while {![string equal $char $delim_e]} {

                append esc $char

                set char [string range $input 0 0]

                set input [string range $input 1 end]

              }

              #echo “escape sequence: $esc”

              switch -exact [string range $esc 0 0] {

                “F” {

                    append output $delim_f

                }

                “E” {

                    append output $delim_e

                }

                “S” {

                    append output $delim_s

                }

                “T” {

                    append output $delim_t

                }

                “R” {

                    append output $delim_r

                }

                “X” {

                    scan [string range $esc 1 end] “%x” x

                    append output [format “%c” $x]

                }

                default {

                    #echo unknown escape sequence $esc

                }

              }

          } else {

              append output $char

          }

        }

        return $output

      }

    Viewing 0 reply threads
    • Author
      Replies
      • #56297
        Alice Kazin
        Participant

          Please send as an attachment.  Then we can download it.

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