2013-10-07 20 views
-1

我在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,但countertemp值是:

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

期望值爲:

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

有什麼問題以及如何解決?

+1

無法重現您所說的內容。你可以在你的代碼中包含'puts'嗎?你可能會錯放他們? – Jerry

+0

你在哪裏檢查$ counter和$ temp的值? – HirofumiTamori

+0

你是否100%確定你在循環中設置了temp,它看起來像你在循環之外設置它。 – KillianDS

回答

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:…"值看起來對我來說位於。