簡體   English   中英

$:中的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 例如:

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

您對變量b所做的任何操作(更改upvar除外)都將在$a命名的變量上執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM