我在使用选项switch
的{{1}}命令中将变量用作模式时遇到问题。
例如:
-regexp
它不起作用,但直接使用正则表达式namespace eval ::foo {
variable symbols {[-=*+_=$!.\w]}
}
proc ::foo::bar {c} {
variable symbols
switch -regexp $c {
{\(} {
return "("
}
...
$symbols {
ungetc $c
return [consume_number_or_symbol]
}
...
}
}
代替{[-=*+_=$!.\w]}
。
如何在$symbol
switch
选项中使用变量?
答案 0 :(得分:1)
您可以传递多个pattern action
对,而不是单个最后一个参数。见http://tcl.tk/man/tcl8.6/TclCmd/switch.htm。请务必使用反斜杠正确地继续命令:
proc ::foo::bar {c} {
variable symbols
switch -regexp $c \
{\(} {
return "("
} \
$symbols {
ungetc $c
return [consume_number_or_symbol]
}
}
答案 1 :(得分:0)
$符号未扩展,因为它位于{}内。以下代码有效:
proc foo {c} {
set symbols x
switch -regexp $c \
$symbols { puts "1" } \
x { puts "2" } \
} foo x