使用tcl,我想将变量参数传递给函数。 我试过这段代码
proc launch_proc { msg proc_name {params {}} } {
puts "params launch_proc is $params \n"
}
proc test { param } {
puts "param test is $param \n"
launch_proc "1.5.2 test param" test_standard {{*}$param param1 param2 param3"
}
}
test value
- > params launch_proc是{*} $ param param1 param2 param3" $ param未被评估(我使用tcl 8.5)
答案 0 :(得分:2)
Tcl支持使用关键字args
作为参数列表中的最后一个参数。
以下是directly from the wiki示例:
proc demo {first {second "none"} args} {
puts "first = $first"
puts "second = $second"
puts "args = $args"
}
demo one
demo one two
demo one two three four five
结果
first = one
second = none
args =
first = one
second = two
args =
first = one
second = two
args = three four five
您也可以使用扩展语法,这使得以下两次调用等效于demo
。
demo one two three four five
set params {three four five}
demo one two {*}$params
答案 1 :(得分:1)
您正在传递一个列表,需要将每个项目作为参数发送到proc。
proc test {p1 p2 p3} {
puts "$p1 - $p2 - $p3"
}
set value {one two three}
# This should work in tcl 8.5+
test {*}$value
set value {four five six}
# For tcl < 8.5
foreach {p1 p2 p3} $value {
test $p1 $p2 $p3
break
}
# or
set value {seven eight nine}
test [lindex $value 0] [lindex $value 1] [lindex $value 2]
输出:
$ tclsh test.tcl
one - two - three
four - five - six
seven - eight - nine
答案 2 :(得分:0)
您需要upvar
,并在构建launch_proc的参数列表时使用list
proc test {varname} {
upvar 1 $varname value
puts "param test is $varname => $value"
launch_proc "1.5.2 test param" test_standard [list {*}$value par1 par2 par3]
}
proc launch_proc {msg proc_name {params {}}} {
puts "params launch_proc: [join $params ,]"
}
set value {"a b c" "d e f" "g h i"}
test value
param test is value => "a b c" "d e f" "g h i"
params launch_proc: a b c,d e f,g h i,par1,par2,par3