Remove segment base on a value in the segment

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Remove segment base on a value in the segment

  • Creator
    Topic
  • #53995
    Yamil Velez
    Participant

      Hi all

       I am trying to remove a segment that contains ICD10 value and keep just the ICD9.   Below I have the sample message, how I want the message to be after the tcl and I also provide the tcl so far.

      I believe it is something simple but I can’t delete the whole segment and rebuild the message without that segment.

      Thanks you in advance.  

        Here is my sample message with all the ICD

                 

      ‘MSH:;~&:HBOC:H:ESCM:H:201312051550::ADT;A08;44:7149988:T:2.2:7149988::AL::

      EVN:A08:201312051550::

      PID::01369515:1300131;;;H:4050;H4880:TEST;JOHN;””;””:””:19671222:M::1:22 TEST ST;””;NEW YORK;NY;10021;US;C;60~””;””;””;””;””;””;P;””:60:(212)555-6777::ENGLISH;E;:””;””:””;””:70100129;;;H:””:

      PD1::::””;;;;;;;;;;;;;;;;:::””;””:””;””:::””:N:””;””

      PV1::O:””;””;””;H;;””;””:1::;;;H:212;DELAND;JONATHAN;””;;;MD:212;DELAND;JONATHAN;””;;;MD::FTA::::1::””:212;DELAND;JONATHAN;””;;;MD:SVO;;H:70100129;;;H:6::::””::::::::::::””;””:::H:””;””::::201312051504:””::::

      PV2::””::””:””:””::””:””:””:””:N:0:””

      DG1:0:I9:564.1:IRRITABLE

      DG1:1:I9:564.1:IRRITABLE

      DG1:0:I10:A00.0:Cholera

      DG1:1:””:A00.1:Cholera

      GT1:1:4050:TEST;JOHN;””;””::22 TEST ST;””;NEW YORK;NY;10021;US;C;60~””;””;””;””;””;””;P;””:(212)555-6777::19671222:M::S:””:::1:::::

      IN1:1:040:040:WORK COMP:””;””;””;””;””;””;C;””:””:””:””:””:::””:””::C;;:TEST;JOHN;””::19671222::::::::::””::::::::””:::::::M::””

      I would like the message to be like below

      ‘MSH:;~&:HBOC:H:ESCM:H:201312051550::ADT;A08;44:7149988:T:2.2:7149988::AL::

      EVN:A08:201312051550::

      PID::01369515:1300131;;;H:4050;H4880:TEST;JOHN;””;””:””:19671222:M::1:22 TEST ST;””;NEW YORK;NY;10021;US;C;60~””;””;””;””;””;””;P;””:60:(212)555-6777::ENGLISH;E;:””;””:””;””:70100129;;;H:””:

      PD1::::””;;;;;;;;;;;;;;;;:::””;””:””;””:::””:N:””;””

      PV1::O:””;””;””;H;;””;””:1::;;;H:212;DELAND;JONATHAN;””;;;MD:212;DELAND;JONATHAN;””;;;MD::FTA::::1::””:212;DELAND;JONATHAN;””;;;MD:SVO;;H:70100129;;;H:6::::””::::::::::::””;””:::H:””;””::::201312051504:””::::

      PV2::””::””:””:””::””:””:””:””:N:0:””

      DG1:0:I9:564.1:IRRITABLE

      DG1:1:I9:564.1:IRRITABLE

      GT1:1:4050:TEST;JOHN;””;””::22 TEST ST;””;NEW YORK;NY;10021;US;C;60~””;””;””;””;””;””;P;””:(212)555-6777::19671222:M::S:””:::1:::::

      IN1:1:040:040:WORK COMP:””;””;””;””;””;””;C;””:””:””:””:””:::””:””::C;;:TEST;JOHN;””::19671222::::::::::””::::::::””:::::::M::””

      Here is the code I have so far

      proc removeicd10 { args } {

         set mode [keylget args MODE]

         set context [keylget args CONTEXT]

         switch -exact — $mode {

      start {

         return “” ;# no specific startup

      }

      run {

         set ibid [keylget args MSGID]

         if {$context != “sms_ib_data”} {

      echo “No reply generated; wrong context $context”

      return “{CONTINUE $ibid}”

         }

         set datList [datlist]

         ; # first get control id from inbound message

         set msg [msgget $ibid]

                 set segmentlist [split $msg r]

                 set newSegmentList “”

                 foreach segment $segmentlist {

                    if {[crange $segment 0 2] == “DG1”} {

                       set fieldlist [split [lindex $segment 0] :]

       set temp1 $fieldlist

       set temp2 [lsearch -all -inline $temp1 I10]  

       echo $temp1

               

       

                    set segment [join $fieldlist :]

       #set temp  [lsearch -all -inline -not $fieldlist I10]  

       #echo $temp

                }

                    set newSegmentList [lappend newSegmentList $segment]

                                        }

                 set newSegmentList [join $newSegmentList r]

                 msgset $ibid $newSegmentList

             ;#  msgdump $ibid

         return “{CONTINUE $ibid}”

      }

      shutdown {

         # Doing some clean-up work

      }

      default {

         return “” ;# don’t know what to do

      }

         }

      }

    Viewing 13 reply threads
    • Author
      Replies
      • #79803
        SREEKANTH CHANDIKA
        Participant

          Read the segments into a list

          Find the index of segment that starts with DG1 and has DG1-2 as ICD10.use lsearch -all option.

          Replace the original segment list by removing the segment found above.Use something like  set segments [lreplace $segments $removeIndex $removeIndex]

          Join segments finally

        • #79804
          Brandon Grudt
          Participant

            I think it goes one step further, as looking at the prospective before and after, the DG1 after the ICD10 is gone as well.

            In this case, you would want to perform a foreach loop on the segment list.  In it, you would do an if statement to grab the segment with the I10.  You would also want to remove the following segment as well.

            This may not be good tcl code but the logic is below.  I’m not solid enough with tcl to write the correct code without testing:

            Code:

            set killvar “0”

            foreach seg $segments {
             set fields [split $seg $sep]
             if { [lindex $fields 2] = “I10” } {
                set killvar “1”
                break
                }
             if { $killvar = “1” } {
                set killvar “0”
                break
                }
             append outbuf ${seg}r
            }

            Someone may supercede me on this, but that is how I would solve the issue.

          • #79805
            Terry Kellum
            Participant

              I think that I would use an “lsearch” with a regexp embedded, and loop over those returns with an lreplace command.  That would reduce your looping over segments and just identify those ICD10 DGs.

              Heck, I’ll bet Charlie could create a command that has no loops….

              It is well worth your time to master the list commands (Chapter 5 in the Welch book. (Practical Programming in TCL and TK, Fourth Edition, Brent Welch et.al.)) .

              Lists are one of the magic parts of TCL that make it so doggone powerful.

            • #79806
              Brandon Grudt
              Participant

                Terry,

                As a rule of thumb, I use lsearch to validate messages or change a single known field.  Alternatively, I use the foreach loop if multiple modifications need to be made, with a few exceptions.  Basically, if I know I need to parse a section of the message, I’ll just throw a loop out there.  If I know exact coordinates, I’ll identify them accordingly.  

                I know that loops slow down the translation, but they make the code so much cleaner.  I’m curious what your opinion is on when to use a loop?

                In looking at my code though, it is wrong no matter what.  The break kills the loop, it does not simply filter out the bad segments.  The way to do that would be to set an else:

                Code:

                set killvar “0”

                foreach seg $segments {
                 set fields [split $seg $sep]
                 if { [lindex $fields 2] = “I10” } {
                    set killvar “1”

                 } elseif { $killvar = “1” } {
                    set killvar “0”
                 } else {
                      append outbuf ${seg}r
                 }
                }

                [/code]

              • #79807
                Terry Kellum
                Participant

                  I could swear that I’ve done this, but as I play with it today, I keep getting a null value in the list..  At this point (or before) I would bounce off into a loop!

                  😉

                • #79808
                  Charlie Bursell
                  Participant

                    Easy enough.  As stated before use lsearch and a loop.  You will need to know the location of each segment.  I will assume you know the segment where the value resides

                    set rmList {}

                    # Loop through each GT1.  If it needs to be removed, save location

                    foreach loc [lsearch -all -regexp $segments {^GT1}]  {

                         set GT1 [split [lindex $segments $loc] $fldSep]

                         

                          if {one of the flds conatin ICD10} { lappend rmList $loc}

                    }

                    # Very important to remove from the end so as not to skew location

                    foreach loc [lsort -integer -decreasing $rmList] {

                          lvarpop segments $loc

                    }

                    msgset $mh [join $segments r]

                  • #79809
                    Yamil Velez
                    Participant

                      Thank you all for the great suggestion and help

                    • #79810
                      Terry Kellum
                      Participant

                        Cool Beans Charlie!!!

                        I have never heard of lvarpop before.  I’m sticking that one in my tool kit for sure!!!!

                      • #79811
                        Brandon Grudt
                        Participant

                          Terry Kellum wrote:

                          Cool Beans Charlie!!!

                          I have never heard of lvarpop before.

                        • #79812
                          Terry Kellum
                          Participant

                            I’m on 5.6.  I show that in my tcl/lib directory with the same time stamp as when I installed Cloverleaf.  

                            Just checked with hcitcl.  It’s in there like Prego.

                          • #79813
                            John Stafford
                            Participant

                              I am trying to do something similar. I have a list of segments that need to be removed.

                              set seglist2

                            • I am searching through the message for each of these segments and copying their indices. A problem occurs when the segment repeats, as there are two indices in one member of the list, and the following error is thrown:

                              Code:

                              [0:TEST] Tcl error:
                              msgId = message0
                              proc = ‘tps_adt_trim_segs’
                              args = ”
                              result = ”
                              errorInfo: ‘
                              expected integer but got “6 7”
                                 while executing
                              “lsort -integer -decreasing $rmList”
                                 (procedure “tps_adt_trim_segs” line 74)
                                 invoked from within
                              “tps_adt_trim_segs {MSGID message0} {CONTEXT sms_ib_data} {ARGS {}} {MODE run} {VERSION 3.0}”‘

                              When I try to split the entries in the list on spaces, I get a different error:

                              Code:

                              [0:TEST] Tcl error:
                              msgId = message0
                              proc = ‘tps_adt_trim_segs’
                              args = ”
                              result = ”
                              errorInfo: ‘
                              expected integer but got “{6”

                              What would be the most effective way to identify and split those values apart?

                        • #79814
                          bill bearden
                          Participant

                            John,

                            Try this slight modification to Charlie’s code. lsearch with the -all switch will return a list of indexes when it finds multiple matches. You probably need to iterate through the result of the lsearch, just in case it returned a list and not a single value.

                            Code:

                            set rmList {}
                            # Loop through each GT1.  If it needs to be removed, save location
                            foreach locList [lsearch -all -regexp $segments {^GT1}]  {
                              foreach loc $locList {
                                set GT1 [split [lindex $segments $loc] $fldSep]
                               
                                 if {one of the flds conatin ICD10} { lappend rmList $loc}
                              }
                            }

                            # Very important to remove from the end so as not to skew location
                            foreach loc [lsort -integer -decreasing $rmList] {
                                 lvarpop segments $loc
                            }

                            msgset $mh [join $segments r]

                          • #79815
                            John Stafford
                            Participant

                              bill bearden wrote:

                              John,

                              Try this slight modification to Charlie’s code. lsearch with the -all switch will return a list of indexes when it finds multiple matches. You probably need to iterate through the result of the lsearch, just in case it returned a list and not a single value.

                              set rmList {}
                              # Loop through each GT1.
                              [code]set rmList {}
                              # Loop through each GT1.

                            • #79816
                              Marc Pleijers
                              Participant

                                Hi All,

                                You can also use the following construction (suppose doing a trick with OBX):

                                # create list of all OBX positions

                                set OBXlocationList [lsearch -all -regexp $segments {^OBX}]

                                # create a list of all the OBX content

                                set OBXcontentList [lsearch -all -inline -regexp $segments {^OBX}]

                                #now you can loop through both lists at the same time without problems

                                # both list have the same length

                                foreach loc $OBXlocationList cont $OBXcontentList {

                                   #   in your loop the segment location is available in $loc

                                   #   in your loop the segment content is available in $cont

                                   #   so you don’t need a lindex anymore in your loop

                                   echo $loc

                                   echo $cont

                                  }

                                Best regards

                                Marc

                                Marc Pleijers
                                Senior Integration Consultant
                                Enovation BV
                                The Netherlands

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