Messin’ With TCL

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Messin’ With TCL

  • Creator
    Topic
  • #51430

    v5.5 on Windows….

    I have an interesting problem – an inbound which sends in results (imaging reports) in an ORU format where the text “lines” of the report are listed as repeating elements in OBX-5, eg:

    Code:

    OBX||FT|^|000852171AMI|PATIENT HISTORY~Patient is postmenopausal, has history of breast cancer at age 57, has history~of high-risk lesion on a previous biopsy at age 56, and had first child at ~age 32.~No known family history of cancer.|

    Each “repeat” (~) gets treated as a soft return so that the above text would look like so in the destination system:

    PATIENT HISTORY

    Patient is postmenopausal, has history of breast cancer at age 57, has history

    of high-risk lesion on a previous biopsy at age 56, and had first child at

    age 32.

    No known family history of cancer.

    All of a sudden, the receiving system has decided that any incoming reports of this nature have to have lines that are 68 characters or less (in other words, each occurrence of OBX-5 needs to be less than 68). This basically means I have to take the full text inside of OBX-5, remove the existing “~” characters, and then put them back in at intervals of 68.

    Now, using an xlate proc I can get pretty far with this but can’t seem to get over the last hump. Here’s the tcl I cobbled together so far:

    Code:

    set myIn0 [lindex $xlateInVals end]

    set myIn2Len [string length $myIn0]

    set myIn0 [string map {~~ @@ ~ ” “} $myIn0]
    set myIn0 [string map {@@ ~~} $myIn0]

    set LoopLimit [expr [string length $myIn0]/68]

    set myOut “”

    for {set LoopNum 0} {$LoopNum < [expr $LoopLimit + 1]} {incr LoopNum} { lappend myOut [concat [string range $myIn0 [expr $LoopNum * 68] [expr [expr $LoopNum * 68] + 67]] ~] } set xlateOutVals $myOut

    The problem is, if I echo out the result of this code I get:

    Code:

    {~~PATIENT HISTORY Patient is postmenopausal, has history of breast c ~} {ancer at age 57, has history of high-risk lesion on a previous biops ~} {y at age 56, and had first child at  age 32. No known family history ~} {of cancer. ~}

    So that I know that it is “working”, insofar as it is breaking the line up where I want it to (also notice that I am keeping “~~” characters where they are since they split the thing up into “paragraphs” and I wanted to keep that formatting intact). But, sumptin’ ain’t right because the outbound transaction only yields:

    Code:

    OBX|1|TX|AWIDM.77056^GDT||~~PATIENT HISTORY Patient is postmenopausal, has history of breast c ~

    This obviously has something to do with lists and lappends and instances of the component and so forth…but I’m not familiar enough with tcl to know how to deal with this.

    What would an expert do?

    Also, I know I probably ultimately clean it up even more so that it is not breaking in the middle of words…but I figured I’d save that for last after I figure out the bigger problem of why I’m not getting the whole string back.

    Thanks for any help/ideas. Anything will increase my knowledge at this point since I’m already at the edge of my envelope on this one.

Viewing 6 reply threads
  • Author
    Replies
    • #70248

      Looks like I should probably change the end of that tcl to:

      Code:

      set xlateOutVals [list $myOut]

      Then I get “everything”, but still those nasty braces:

      Code:

      OBX|1|TX|AWIDM.77056^GDT||{~~PATIENT HISTORY Patient is postmenopausal, has history of breast c ~} {ancer at age 57, has history of high-risk lesion on a previous biops ~} {y at age 56, and had first child at  age 32. No known family history ~} {of cancer. ~}

      How do I get rid of that?

    • #70249
      James Cobane
      Participant

      Dan,

      Try this:

      set xlateOutVals

        ]

        I believe you actually want to join all of you individual list elements (hence the braces around each item) into a single value before putting into xlateOutVals.

        Hope this helps.

        Jim Cobane

        Henry Ford Health

      1. #70250
        David Barr
        Participant

        The reasons you have the braces is because you have created a list.  James suggested that you use “join” to convert the list back to a single string, but it would probably be easier to keep everything as a string in the first place by using “append” instead of “lappend”.

      2. #70251
        Robert Kersemakers
        Participant

        Hmmm… You are breaking words.

        Is the receiving system going to put them back together or just show the lines like with the broken words?

        Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

      3. #70252

        Thanks everybody. I am getting pretty close.

        David/James, I tried join and append (as opposed to lappend) but they didn’t seem to work. Robert, yeah I knew about the word-breaking but I needed to fix my bigger problem first which was the list/braces.

        Here is what I have now. I know this is probably exremely inelegant, but through more than a few hours of trial and error this is what I have come up with:

        Code:

        set myIn0 [lindex $xlateInVals end]

        set myIn0 [string map {~~ @@ ~ ” “} $myIn0]
        set myIn0 [string map {@@ ~~} $myIn0]
        set myOut “”
        set myEnd [string length $myIn0]
        set LoopStart 0

        while {[expr $LoopStart + 68] < [expr $myEnd + 68]} {
        set LoopStart [expr $LoopStart – 1]
        set LoopEnd [string wordstart $myIn0 [expr $LoopStart + 67]]
        lappend myOut [concat [string range $myIn0 [expr $LoopStart] [expr $LoopEnd – 1]] ~]
        set LoopStart [expr $LoopEnd]
        incr LoopStart
        }

        set myOut [string replace $myOut [expr [string length $myOut] -3] [string length $myOut] .]
        set myOut [string map {"{" "" "}" ""} $myOut]

        set xlateOutVals [list $myOut]

        The set myOut [string map {“{” “” “}” “”} $myOut] near the end removes the braces, and the set xlateOutVals

          seems to get the whole string out.

          I re-worked the main part in the middle, using a while loop (instead of the for I had before). Basically I’m using an internal counter to keep my place as I move forward in the string, and I “back up” using the word start so I don’t break a word in half. One problem I did have was the word start (I believe) was causing this process to always clip off the period at the very end, so I used the

        Code:
        set myOut [string replace $myOut [expr [string length $myOut] -3] [string length $myOut] .]

        to put it back on at the end (before the bracket removal and list output).

        My last “problem”, should I choose to go that far, is that this still occasionally breaks lines at less than optimal places. I think I will add another section that looks to see if there is a “~~” in the current “line” and then resets the pointer to that spot and moves to the next “line” so that instead of:

        Code:

        but still prominent in  size.

        ASSESSMENT: BI-RADS 5: HIGHLY
        SUGGESTIVE OF MALIGNANCY

        RECOMMENDATION Breast MRI of both breasts.

        I would get:

        Code:

        but still prominent in  size.

        ASSESSMENT: BI-RADS 5: HIGHLY SUGGESTIVE OF MALIGNANCY

        RECOMMENDATION Breast MRI of both breasts.

        It also seems to occasionally break things like:

        Code:

        January 10
        , 2009

        So I might need to figure a way around that too (maybe account for the comma or something). But that is optional, at least for now. Better the poor wrapping than have all of Radiology be down because of a report with more than 68 characters in a line (great system, that one).

        Thanks for all your tips. This has been an unpleasant but educational experience!

      4. #70253
        David Barr
        Participant

        What happens when you change the “lappend” to “append” and get rid of the “string map” and “string replace” lines?  You said it didn’t work, but I’m not sure what the problem was.

      5. #70254

        When I did just “append” (instead of “lappend”), I was back to only getting the first x number of characters back into the message – basically whatever text would be represented in the first set of braces {} in the “lappend” method.

        So, get this – after all the work and tcl learning, we find an easy way in the sending system to have it do the wrapping at 68 characters before it even sends the message out. Aaaaah!

        But, like I said, at least I did get to learn some tcl stuff, and now it wraps “properly” since the sending system is handling the line formatting.

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

    Forum Statistics

    Registered Users
    5,125
    Forums
    28
    Topics
    9,294
    Replies
    34,439
    Topic Tags
    287
    Empty Topic Tags
    10