foreach increment problem

Homepage Clovertech Forums Read Only Archives Cloverleaf Tcl Library foreach increment problem

  • Creator
    Topic
  • #50929
    Garrett Fletcher
    Participant

    Hello,

    I am using a foreach loop on a list of segments to do some processing. I am trying to increment the foreach loop’s counter to skip certain segments. When ever i use the ‘incr countSegs1’ command I get an error in the testing module. The ‘incr countSegs2’ command does not cause an error because it is not the foreach loop’s counter. Here is my code. Any help would be appreciated.

    Code:

    ######################################################################
    # Name:     stringFindReplaceField
    #
    # Purpose:  If the OBR-4 field matches, this will search a specified field for a string and put a
    #           predefined string in another field in a differnt field in
    #  the same segment
    #          
    # Args:     tpc keyedlist containing the following keys:
    #           MODE    run mode (”start”, “run” or “time”)
    #           MSGID   message handle
    #           ARGS    keyed list of user arguments containing:
    # SEGID the name of the segment containing the field
    # INDEX the HL7 index of the field to check
    # #LABTYPE Type of lab in OBR 4 (if value contains spaces enclose in {})
    # SEARCH String to search for (if value contains spaces enclose in {})
    #######################################################################

    proc stringFindReplaceFieldTst { args } {
    keylget args MODE mode
    switch -exact — $mode {

    start {
    #Preform Special Init Functions
    }

    run {
    #Get Args and put them in variables

    keylget args MSGID mh
    keylget args ARGS.SEGID seg
    keylget args ARGS.INDEX fieldno
    #keylget args ARGS.LABTYPE labType
    keylget args ARGS.SEARCH searchString

    #Set normal variables

    set countSegs2 0
    set segmentCounter 0
    set countIndex 0
    set msg [msgget $mh]
        set segmentList [split $msg “r”]

    #Variables The OBR Segment

    set obr “OBR”
    set four 4
    set bCult “MIC^BLD CULT SCREEN”
    #set testsegment [getHL7Segment $msg $obr]
    #set testfield [getHL7Field $testsegment $four]

    #Test to see if this is the result you want to search

    #This foreach statement makes sure each segment is checked for the desired string
    foreach countSegs1 $segmentList {

    if {[string match “$obr*” [lindex $segmentList $countSegs2]]} {

    if {[string match “*$bCult*” [lindex $segmentList $countSegs2]]} {

    incr countSegs1
    incr countSegs2

    while {[string match “$seg*” [lindex $segmentList $countSegs2]]} {

    if {[string match “*$searchString*” [lindex $segmentList $countSegs2]]} {

    #breaks down the segment, inserts the new text, puts it back together
    set breakdown [split [lindex $segmentList $countSegs2] “|”]
    set newSeg [lreplace $breakdown 2 2 $searchString]
    set newSeg [join $newSeg “|”]
    set segmentList [lreplace $segmentList $countIndex $countSegs2 $newSeg]
    set msg [join $segmentList n]

    }

    incr countSegs1
    incr countSegs2

    }
    }
    }

    incr countSegs2

    }

    echo “$msg n”
    return “{CONTINUE $mh}”

    }

    shutdown {
    #cleanup procedures
    }

    default {
        error “Unknown mode ‘$mode’ in stringSearch proc”
            }

    }

    }


    Thank you,

    Garrett

Viewing 2 reply threads
  • Author
    Replies
    • #68047

      The foreach statement loops through a list of values. It does not work like a for statement which uses a counter like i.

      Code:

      set myList “a b c d e f g”

      foreach letter $myList {
         puts $letter
      }

      If you want to skip a value in the list, use the continue statement.

      Code:

      set myList “a b c d e f g”

      foreach letter $myList {
         if {$letter == “d”} {continue}
         puts $letter
      }

      -- Max Drown (Infor)

    • #68048
      Garrett Fletcher
      Participant

      Thank you, that is helpful.

      Also, I was using the variable wrong. I thought it was a counter that shows what iteration you were on. Turns out it just returns the current item you are on in your list.

      G

    • #68049

      Exactly.

      If you wanted to actually use a counter, you can. Initialize a variable outside of the loop and then increment the variable inside the loop.

      set myList “a b c d e f g”
      set cntr “0”

      foreach letter $myList {
      [code]set myList “a b c d e f g”
      set cntr “0”

      foreach letter $myList {

      -- Max Drown (Infor)

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

Forum Statistics

Registered Users
5,092
Forums
28
Topics
9,274
Replies
34,365
Topic Tags
281