tcl中的棘手的var验证

hey*_*hey 2 tcl

根据我的输入,我有一个存在或不存在的变量,所以我做了一个if:

if { [info exists $var1] } {
    puts "Here we are, Born to be kings, We're the princes of the universe ..."
}
Run Code Online (Sandbox Code Playgroud)

但是当我启动我的脚本时,我收到了错误日志:

can't read "var1": no such variable
while executing
"info exists $var1"
Run Code Online (Sandbox Code Playgroud)

怎么了?

Joh*_*uhn 7

使用 if { [info exists var1] } {...

在调用之前替换变量,并且info也不例外.所以传递变量的名称而不是它的值.

  • @heyhey,另见[本讨论](http://stackoverflow.com/q/7299320/720999) (2认同)