user707549
user707549

Reputation:

Any filter we can use in tcl script?

I have a question about the for loop,

for {{set loop 0} {$loop < 100} {incr loop}} {
    #do someting here
}

loop goes from 0 to 99, and I do something for each value of loop, but if the loop is 3, I will skip it, so, is there any filter in tcl to achieve it or we should write it as:

for {{set loop 0} {$loop < 100} {incr loop}} {
    if {loop != 3} {
        #do someting here
    }
}

Upvotes: 2

Views: 946

Answers (3)

glenn jackman
glenn jackman

Reputation: 246837

The first, third and fourth arguments to for can be arbitrary scripts, so you could do this:

for {set i 0} {$i < 100} {incr i [expr {$i == 2 ? 2 : 1}]} {
    do stuff with $i ...
}

Upvotes: 1

kostix
kostix

Reputation: 55483

% proc xiter {varName "over" a z "excluding" filter body} {
    upvar 1 $varName i
    set excl [lsort $filter]
    for {set i $a} {$i < $z} {incr i} {
      if {[lsearch -exact -sorted $excl $i] < 0} {
        uplevel 1 $body
      }
    }
  }
% xiter loop over 0 10 excluding {5 3 8} {
  puts $loop
}
0
1
2
4
6
7
9

Upvotes: 2

Bryan Oakley
Bryan Oakley

Reputation: 386010

You can use the "continue" command. For example:

for {set loop 0} {$loop < 100} {incr loop} {
    if {$loop == 3} continue
    # do something here
}

Upvotes: 5

Related Questions