admin管理员组文章数量:1410674
I have a total of 750 count. And each set is 5. Instead of doing many for loop, for {set i 0} {i < 5}, for {set i 5} {i < 10}, for {set i 10} {i < 15}..... Every 5 count, will insert "GOOD".
for {set i 0} {i < 750} {incr i} {
puts $i
if { i * 15} {
puts GOOD
}
Here is the output I am expecting:
0 1 2 3 4 GOOD
5 6 7 8 9 GOOD
10 11 12 13 14 GOOD
15 16 17 18 19 GOOD
Last output 745 746 747 748 749 GOOD
I have a total of 750 count. And each set is 5. Instead of doing many for loop, for {set i 0} {i < 5}, for {set i 5} {i < 10}, for {set i 10} {i < 15}..... Every 5 count, will insert "GOOD".
for {set i 0} {i < 750} {incr i} {
puts $i
if { i * 15} {
puts GOOD
}
Here is the output I am expecting:
0 1 2 3 4 GOOD
5 6 7 8 9 GOOD
10 11 12 13 14 GOOD
15 16 17 18 19 GOOD
Last output 745 746 747 748 749 GOOD
Share Improve this question edited Mar 7 at 9:03 Chang asked Mar 7 at 8:17 ChangChang 12 bronze badges 2- I am not an expert in TCL but it looks like you use the same counter variable in an nested loop. What happens if you set the second "i" to "j" as example? so the second loop states: for{set j 0) {j<5} {incr j} {puts $j} – LMA Commented Mar 7 at 8:25
- Your code is incomplete - missing closing } – thomas Commented Mar 13 at 7:03
2 Answers
Reset to default 1Your main problem seems to be using the wrong condition; $i * 15
will be true for every non-zero value of i
(Tcl interprets non-zero numbers as true). I think you are more likely to want $i % 5 == 4
, which checks if the remainder of dividing the value in i
by 5 is 4.
for {set i 0} {$i < 750} {incr i} {
puts -nonewline "$i "
if { $i % 5 == 4 && $i != 0} {
puts "GOOD "
}
}
added two conditions. and use nonewline to get your ouput format.
$i % 5 == 4 && $i != 0
本文标签: for loop with increment tclStack Overflow
版权声明:本文标题:for loop with increment tcl - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744942436a2633571.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论