尝试创建嵌套for循环,生成缺少的参数错误

时间:2011-01-03 19:54:26

标签: tcl

我试图用Tcl自学编程。 (我想更熟悉语言,了解别人的代码 - SCID国际象棋) 我自己设定的任务是激励我学习Tcl是为了解决8皇后问题。 我创建程序的方法是过度“原型化”解决方案。 所以。 我要在第2行嵌套一个持有q pos的for循环    在第一行持有q pos的for循环内部

这是我的代码

set allowd 1
set notallowd 0

for {set r1p 1} {$r1p <= 8} {incr r1p } {
    puts "1st row q placed at $r1p" 
    ;# re-initialize r2 'free for q placemnt' array after every change of r1 q pos:
    for {set i 1 } {$i <= 8} {incr i} { set r2($i) $allowd    }

    for { set r2($r1p) $notallowd ; set r2([eval $r1p-1]) $notallowd ;
           set r2([eval $r1p+1]) $notallowd ; set r2p 1}   {$r2p <= 8} {
         incr r2p ;# end of 'next' arg of r2 forloop
        }
        ;# commnd arg of r2 forloop placed below: 
        {puts "2nd row q placed at $r2p"    
    }
} 

我的问题是,当我运行代码时,解释器正在以致命错误中止: “错误的#args应该用于开始测试下一个命令。

我已经查看了几次我的代码并且看不到我错过了任何for循环参数。

1 个答案:

答案 0 :(得分:5)

在最后一个for循环中的命令之前的回车是什么让你。从first syntax rule on the Tcl man page开始,“半冒号和换行符是命令分隔符,除非引用如下所述。”顺便说一句,你的评估应该是expr's。

这对我有用:

set allowd 1
set notallowd 0

for {set r1p 1} {$r1p <= 8} {incr r1p } {
    puts "1st row q placed at $r1p" 
    ;# re-initialize r2 'free for q placemnt' array after every change of r1 q pos:
    for {set i 1 } {$i <= 8} {incr i} { set r2($i) $allowd    }

    for { set r2($r1p) $notallowd ; set r2([expr $r1p-1]) $notallowd ;
        set r2([expr $r1p+1]) $notallowd ; set r2p 1}   {$r2p <= 8} {
        incr r2p ;# end of 'next' arg of r2 forloop
    } {
        # commnd arg of r2 forloop placed below: 
        puts "2nd row q placed at $r2p"    
    }
}