Tcl 是否有一种方法可以在不使用args的情况下传递可变数量的参数
代码如下:Tcl 是否有一种方法可以在不使用args的情况下传递可变数量的参数,tcl,Tcl,代码如下: >cat /tmp/test_args.tcl proc t1 {args} { return $args } proc t2 {args} { puts "t2:[llength $args]" return set len [llength $args] if {$len == 1} { proc_len_1 [lindex $args 0] } elseif {$len == 2} { pr
>cat /tmp/test_args.tcl
proc t1 {args} {
return $args
}
proc t2 {args} {
puts "t2:[llength $args]"
return
set len [llength $args]
if {$len == 1} {
proc_len_1 [lindex $args 0]
} elseif {$len == 2} {
proc_len_2 [lindex $args 0] [lindex $args 1]
} else {
proc_len_x $args
}
}
set tup3 [t1 1 2 3 4 5 6]
puts "before calling t2:[llength $tup3]"
t2 $tup3
t2 100
t2 100 200
以下是输出:
>tclsh /tmp/test_args.tcl
before calling t2:6
t2:1
t2:1
t2:2
我使用的是TCL8.6
您可以看到,在调用t2之前,$tup3是一个列表,但是proc t2接收$tup3作为一个值,因此proc t2接收的不是一个值列表,而是一个值列表
但是,正如“return”之后的代码所示,proc t2的目的是处理不同数量的参数,并根据参数的数量执行不同的操作。现在,使用列表变量调用t2和使用文本调用t2的处理是相同的。这就是问题所在
我能想到的唯一解决办法是改变
t2 $tup3
到
但我有一个限制:$tup3在传递到不同的进程时需要保持不变。例如,我可以有这样的proc,它也需要$tup3:
proc t3 {arg1} {
}
t3 $tup3
因此,理想情况下,如果我能使“args”不将值包装到列表中,那么我的问题就解决了。我知道这就是TCL的工作原理
也许我已经回答了我自己的问题,或者我不知道我在寻找什么。如果你看到确实有解决办法,请让我知道
谢谢。如果您想传递列表,只需将其作为参数接受即可:
proc a { mylist } {
b $mylist
}
proc b { mylist } {
foreach {k} $mylist {
puts $k
}
}
set tup3 [t1 1 2 3 4 5 6]
a $tup3
编辑:
对于数量可变的参数,使用命令行处理是最简单的
proc a { args } {
array set arguments $args
if { [info exists arguments(-tup3)] } {
puts "tup3: $arguments(-tup3)"
}
if { [info exists arguments(-tup1)] } {
puts "tup1: $arguments(-tup1)"
}
parray arguments
}
set tup3 [list 1 2 3 4 5 6]
a -tup1 test1 -tup3 $tup3 -tup2 test2
如果要传递列表,只需将其作为参数接受即可:
proc a { mylist } {
b $mylist
}
proc b { mylist } {
foreach {k} $mylist {
puts $k
}
}
set tup3 [t1 1 2 3 4 5 6]
a $tup3
编辑:
对于数量可变的参数,使用命令行处理是最简单的
proc a { args } {
array set arguments $args
if { [info exists arguments(-tup3)] } {
puts "tup3: $arguments(-tup3)"
}
if { [info exists arguments(-tup1)] } {
puts "tup1: $arguments(-tup1)"
}
parray arguments
}
set tup3 [list 1 2 3 4 5 6]
a -tup1 test1 -tup3 $tup3 -tup2 test2
Tcl的设计使得过程(或C定义的命令)很难检查调用它的语法。这种方式完全是经过深思熟虑的,因为它使任意编写命令变得非常容易。建议需要特别关注如何调用它们的语法的命令执行额外的步骤来处理它们的参数,并在调用者的环境中进行适当的调用(在C中很简单,在Tcl过程中由于额外的堆栈框架而稍微复杂)
这张照片是:
my input string was 'foo bar boo'
my value is 'foo bar boo'
its length is 3
my input string was '$x'
my value is '123'
its length is 1
您可以在过程中获得调用语法,但除了调试之外,这是非常不推荐的,因为它往往会生成通常会使处理变得烦人的信息。以下是您获得它的方式:
proc example inputString {
puts "I was called as: [dict get [info frame -1] cmd]"
}
# To show why this can be awkward, be aware that you get to see *all* the details...
example {foo bar boo}
example "quick brown fox"
example [expr {1 + sqrt(rand())}]
set x 123
example $x
其中打印:
I was called as: example {foo bar boo}
I was called as: example "quick brown fox"
I was called as: example [expr {1 + sqrt(rand())}]
I was called as: example $x
上面的第一种方法,传入一个您自己解析的文本(根据需要在Tcl的适当帮助下),被认为是好的Tcl风格。在Tcl中嵌入一种语言(可以是Tcl本身,也可以是其他语言;人们已经展示了使用嵌入式C和Fortran的方法,没有理由认为任何其他语言都会是一个大问题,尽管获取有用的求值语义有时会……很棘手),这是绝对好的,使过程(或C定义的命令)很难检查如何调用它的语法。这种方式完全是经过深思熟虑的,因为它使任意编写命令变得非常容易。建议需要特别关注如何调用它们的语法的命令执行额外的步骤来处理它们的参数,并在调用者的环境中进行适当的调用(在C中很简单,在Tcl过程中由于额外的堆栈框架而稍微复杂)
这张照片是:
my input string was 'foo bar boo'
my value is 'foo bar boo'
its length is 3
my input string was '$x'
my value is '123'
its length is 1
您可以在过程中获得调用语法,但除了调试之外,这是非常不推荐的,因为它往往会生成通常会使处理变得烦人的信息。以下是您获得它的方式:
proc example inputString {
puts "I was called as: [dict get [info frame -1] cmd]"
}
# To show why this can be awkward, be aware that you get to see *all* the details...
example {foo bar boo}
example "quick brown fox"
example [expr {1 + sqrt(rand())}]
set x 123
example $x
其中打印:
I was called as: example {foo bar boo}
I was called as: example "quick brown fox"
I was called as: example [expr {1 + sqrt(rand())}]
I was called as: example $x
上面的第一种方法,传入一个您自己解析的文本(根据需要在Tcl的适当帮助下),被认为是好的Tcl风格。在Tcl中嵌入一种语言(可以是Tcl本身,也可以是其他语言;人们已经展示了如何使用嵌入式C和Fortran,没有理由期望任何其他语言会成为一个大问题,尽管获取有用的求值语义有时可能会……很棘手)非常好。proc a必须能够接收值列表或一些单独的文本:@my_question如果只有一个值,您可以将其作为一个列表和一个条目传递吗?proc a必须能够接收值列表或一些单独的文本:@my_question如果只有一个值,您可以将其作为一个带有一个条目的列表传递吗?当前过程的可变参数数的选项是可选参数(该参数提供了默认值)和
args
。当前过程的可变参数数的选项是可选参数(其中参数提供了默认值)和args
。