简体   繁体   English

$:中的Tcl变量替换

[英]Tcl variable substitution inside $::

If I define the following list: 如果我定义以下列表:

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

I can check if the variable ::inputfile exists using: 我可以使用以下命令检查变量:: inputfile是否存在:

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

But I cannot access the value of ::inputfile using: 但是我无法使用以下方式访问:: inputfile的值:

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

I got the error: can't read "::": no such variable 我收到错误:无法读取“ ::”:没有这样的变量

Is there a way to get this substitution done? 有没有办法完成这种替换? I mean turn $::[lindex $a 0] into $::inputfile so I can access the value of ::inputfile 我的意思是将$ :: [lindex $ a 0]转换为$ :: inputfile,以便我可以访问:: inputfile的值

Thanks a lot 非常感谢

The $ is a shortcut, and can't do double substitution. $是快捷方式,不能进行双替换。 Use the set command: 使用set命令:

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

Apart from the longhand form of $ , the one-argument form of the set command, there is also the option to create an alias of the variable (with a known name) using upvar 0 . 除了$的单字形式( set命令的单参数形式)之外,还可以使用upvar 0创建变量(具有已知名称)的upvar 0 For example: 例如:

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

Anything you do to the variable b (except changing the upvar ) will be performed on the variable that was named by $a . 您对变量b所做的任何操作(更改upvar除外)都将在$a命名的变量上执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM