Variables tcl过程中的变量/数组
如何在过程之外传递一些变量/数组 假设我的过程'myproc'带有输入参数{abcde},例如Variables tcl过程中的变量/数组,variables,tcl,procedures,Variables,Tcl,Procedures,如何在过程之外传递一些变量/数组 假设我的过程'myproc'带有输入参数{abcde},例如 myproc {a b c d e} { ... do something (calculate arrays, lists and new variables) } 在这个过程中,我想从变量a-e中计算一个数组phiN(1),phiN(2),…phiN(18),它本身就是一个列表,例如 set phiN(1) [list 1 2 3 4 5 6 7 8 9]; (假设值1-9是
myproc {a b c d e} {
... do something
(calculate arrays, lists and new variables)
}
在这个过程中,我想从变量a-e中计算一个数组phiN(1),phiN(2),…phiN(18),它本身就是一个列表,例如
set phiN(1) [list 1 2 3 4 5 6 7 8 9];
(假设值1-9是从输入变量a-e中计算出来的)。我想计算一些其他参数α和β
set alpha [expr a+b];
set beta [expr c+d];
无论如何,不,我想在我的过程之外传递这些新的计算变量。与matlab相比,我只想在“函数”之外得到这些变量
[phiN,alpha,beta] = myproc{a b c d e}
有人知道我如何在tcl做生意吗??谢谢 有几个选项:
例如: 如果您返回值,但不返回数组,则这很好
例如:
例如:
范例
有关更多想法(这是关于数组的,但也可以应用于值),请参阅。可能重复您为什么不先搜索?搜索带有“tcl”标签的“数组”和“过程”这两个词的问题会带来一些已经提问和回答的问题。感谢链接。我朝另一个方向搜索。我搜索了如何从一个PROC中传递一个以上的变量,但不特别是数组。有时候这更有意义。
proc myproc {a b c d e} {
set alpha [expr {$a+$b}]
set beta [expr {$c+$d}]
return [list $alpha $beta]
}
lassign [myproc 1 2 3 4 5] alpha beta
proc myproc {phiNVar a b c d e} {
upvar 1 $phiNVar phiN
# Now use phiN as local variable
set phiN(1) [list 1 2 3 4 5 6 7 8 9]
}
# Usage
myproc foo 1 2 3 4 5
foreach i $foo(1) {
puts $i
}
proc myproc {phiNVar a b c d e} {
uplevel 1 $phiNVar phiN
set alpha [expr {$a+$b}]
set beta [expr {$c+$d}]
set phiN(1) [list 1 2 3 4 5 6 7 8 9]
return [list $alpha $beta]
}
lassign [myproc bar 1 2 3 4 5] alpha beta
foreach i $bar(1) {
puts $i
}
proc myproc {a b c d e} {
set alpha [expr {$a+$b}]
set beta [expr {$c+$d}]
set phiN(1) [list 1 2 3 4 5 6 7 8 9]
return [list [array get phiN] $alpha $beta]
}
lassign [myproc 1 2 3 4 5] phiNDict alpha beta
array set bar $phiNDict
foreach i $bar(1) {
puts $i
}
# Use the [dict] command to manipulate the dict directly
puts [dict get $phiNDict 1]