如何在TCL中增加循环中的变量?

时间:2013-10-07 08:43:38

标签: loops tcl

我在TCL中有以下代码:

set counter 1

for {set i 0} {$i < 3} {incr i 1} {
    set temp $counter
    incr temp 1
    incr counter 2 
}

对于每个循环,counter增加2,temp根据counter的值增加1,但counter的值增加{ {1}}是:

temp

预期值为:

counter 1 temp 2 in the first loop
counter 3 temp 3 in the second loop
counter 5 temp 4 in the third loop

问题是什么以及如何解决?

1 个答案:

答案 0 :(得分:0)

这完全取决于您使用值的位置:

set counter 1
for {set i 0} {$i < 3} {incr i 1} {
    set temp $counter
    puts "A: counter = $counter, temp = $temp"
    incr temp 1
    puts "B: counter = $counter, temp = $temp"
    incr counter 2 
    puts "C: counter = $counter, temp = $temp"
}

当我跑步时,我得到:

A: counter = 1, temp = 1
B: counter = 1, temp = 2
C: counter = 3, temp = 2
A: counter = 3, temp = 3
B: counter = 3, temp = 4
C: counter = 5, temp = 4
A: counter = 5, temp = 5
B: counter = 5, temp = 6
C: counter = 7, temp = 6

在我看来,您想要puts "B:…"所在位置的值。