我正在从文本文件中读取行集。这样布置
kCode %title My Title
kCode %content Hello World
pCode %picture MyVar
我定义了MyVar:
set MyVar fox
set code [lindex $line 0] ;#Set code to retrieve xCode from text file
set morph [lindex $line 2] ;#Set morph to the actual value from text file
带有条件if语句
if { $code eq "pCode" } {
puts "I found a picture which is $morph" ;#outputs MyVar
}
但是,我正在努力回忆起在文本文件中定义的变量的值。
我最近的是
set animal [eval $$morph] ;#references MyVar value which is fox as previously defined
但是我明白了
invalid command name "fox"
最终,我只想从定义的MyVar输出或保存到变量“ fox”,但似乎找不到正确的组合。
我确定这很简单,但是..卡住了。
答案 0 :(得分:2)
set
过程接受1或2个参数,并返回变量的实际值:1)变量名称2)要分配给变量的值。如果未指定第二个参数,则该变量将不会更改。因此,最简单的解决方案是仅使用变量名来调用set
过程。在您的示例中:
set animal [set $morph]