我想查找特定文件中是否存在 If 块。
我有一个文件new.tcl如下:
if { [info exists var1] } {
if { "$var1" == "val1" } {
puts "var1 has value as val1"
}
} else {
puts "var1 does not exists"
}
我在另一个 Tcl 函数中读取此文件并尝试通过regexp
函数匹配 if 块,并且此函数中使用的值和变量是变量。
我的实现文件看起来像,
set valueDict [list 'var1' 'val1']
set valDictLen [llength $valueDict]
set myFilePtr [open "new.tcl" "r"]
set myFileContent [read $myFilePtr]
close $myFilePtr
for { set index 0 } { $index < $valDictLen } { incr index 2 } {
set currVar [lindex $valueDict $index]
set currVal [lindex $valueDict [expr $index + 1]]
# I actually want to match the entire if block content here
if { ![regexp "if \{ \[info exists $currVal\] \}" $myFileContent] } {
puts "Code not present"
}
}