如何命令TCL过程使用变量



我试图有一个非常通用的函数,并命令它使用它从外部遇到的变量。我尝试了以下(简化代码),但没有用:

set line "found $find1 at $find2"
do_search $line
proc do_search {line} {
...
if {[regexp $exp $string match find1 find2} {
     puts "$line"
}

然而,我得到的是:found $find1 at $find2,或者,如果我在$find之前不使用,则在调用函数之前查找的值。

考虑到这个regexp是解析文件时while循环的一部分,我不能在调用进程后使用这些值。

你知道怎么做吗?

对于您的确切样式,您需要subst:

if {[regexp $exp $string match find1 find2} {
     puts [subst $line]
}

但是你也可以考虑使用format:

set fmt "found %s at %s"
do_search $fmt
proc do_search {fmt} {
...
if {[regexp $exp $string match find1 find2} {
     puts [format $fmt $find1 $find2]
}

最新更新