$:: 中的 Tcl 变量替换

Tcl variable substitution inside $::

如果我定义如下列表:

set ::listofvariablesn [list {"inputfile" "Input file"}]
set ::inputfile "Somefile or numbers"

我可以使用以下方法检查变量 ::inputfile 是否存在:

foreach a $::listofvariablesn {
   if {[info exists ::[lindex $a 0]]} {
      puts "Ok" 
   }
}

但我无法使用以下方法访问 ::inputfile 的值:

foreach a $::listofvariablesn {   
    if {[checkvl "Input file" $::[lindex $a 0] 1 0 0 0]} { 
        puts "Ok" 
    }
}

我收到错误: 无法读取“::”:没有这样的变量

有没有办法完成这个替换?我的意思是将 $::[lindex $a 0] 变成 $::inputfile 这样我就可以访问 ::inputfile

的值

非常感谢

$是快捷方式,不能做双重替换。 使用set命令:

if {[checkvl "Input file" [set ::[lindex $a 0]] 1 0 0 0]} { 

除了 $ 的普通形式,set 命令的单参数形式外,还可以选择创建变量的别名(具有已知名称),使用upvar 0。例如:

upvar 0 $a b
puts "exists: [info exists b]" 
puts "value: $b" 

您对变量 b 所做的任何操作(更改 upvar 除外)都将在 $a 命名的变量上执行。