Count number of times a segment occurs

Homepage Clovertech Forums Read Only Archives Cloverleaf Tcl Library Count number of times a segment occurs

  • Creator
    Topic
  • #51094
    Mike Keys
    Participant

    I hope this is easy. I’m trying to count the number of times a specific segment occurs in my HL7 message. It’s variable and I will use this value for a for loop variable later in my uproc.

    I can count the number of elements by

    set segmentList [split $msg r]            

    set elements $segmentList

    set j [llength $elements]

    Now, I want to see how many of those elements begin with ZTX and return that as a number.

Viewing 2 reply threads
  • Author
    Replies
    • #68780
      Mike Keys
      Participant

      Well, think I got it figured out. I simply added another foreach loop ahead of the foreach loop that manipulates each segment.

      set j 0

      foreach ztx $segmentList {

      if [cequal [crange $ztx 0 2] ZTX] {

    • #68781
      Charlie Bursell
      Participant

      Much more ergonomic would be:

      set j [llength [lsearch -all -regexp $segmentList {^ZTX}]]

      I am not a big fan of unneccesary looping  ðŸ™‚

    • #68782
      Mike Keys
      Participant

      Thanks Charlie. I had that earlier minus the {} around ^ZTX.

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

Forum Statistics

Registered Users
5,127
Forums
28
Topics
9,299
Replies
34,443
Topic Tags
288
Empty Topic Tags
10