TCL 文本文件字符串到变量
TCL text file string to variable
我正在从文本文件中读取一组行。布局是这样的
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",但似乎找不到正确的组合。
我确定这很简单但是..卡住了。
set
过程接受 1 或 2 个参数和 returns 变量的实际值:1) 变量名称 2) 分配给变量的值。如果没有指定第二个参数,那么变量将不会改变。因此,最简单的解决方案是仅使用变量名调用 set
过程。在你的例子中:
set animal [set $morph]
我正在从文本文件中读取一组行。布局是这样的
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",但似乎找不到正确的组合。
我确定这很简单但是..卡住了。
set
过程接受 1 或 2 个参数和 returns 变量的实际值:1) 变量名称 2) 分配给变量的值。如果没有指定第二个参数,那么变量将不会改变。因此,最简单的解决方案是仅使用变量名调用 set
过程。在你的例子中:
set animal [set $morph]