【发布时间】:2017-07-08 20:44:57
【问题描述】:
我想将一个过程的输出作为参数传递给另一个过程。下面是我尝试过的代码。
proc distance {n1 n2 nd1 nd2} {
set x1 [expr int([$n1 set X_])]
set y1 [expr int([$n1 set Y_])]
set x2 [expr int([$n2 set X_])]
set y2 [expr int([$n2 set Y_])]
set d [expr hypot($x2-$x1,$y2-$y1)]
return [list $nd1 $nd2 $x1 $y1 $x2 $y2 $d]
}
proc processDistances {count threshold {filter ""}} {
global node_
set distances {}
for {set i 1} {$i < $count} {incr i} {
for {set j 1} {$j < $count} {incr j} {
# Skip self comparisons
if {$i == $j} continue
# Apply target filter
if {$filter ne "" && $j != $filter} continue
# Get the distance information
set thisDistance [distance $node_($i) $node_($j) $i $j]
# Check that the nodes are close enough
if {[lindex $thisDistance 6] < $threshold} {
lappend distances $thisDistance
}
}
}
# Sort the pairs, by distances
set distances [lsort -real -increasing -index 6 $distances]
Inverse2 {*}$distances
}
$ns at 8.5 [list processDistances $val(nn) 200 41]
proc Inverse2 {m} {
set result [open R.tr w]
lassign [lindex $m 0 2] x1
lassign [lindex $m 0 3] y1
lassign [lindex $m 0 4] d1
lassign [lindex $m 1 2] x2
lassign [lindex $m 1 3] y2
lassign [lindex $m 1 4] d2
lassign [lindex $m 2 2] x3
lassign [lindex $m 2 3] y3
lassign [lindex $m 2 4] d3
set mt {{? ?} {? ?}}
lset mt 0 0 [expr 2*($x1-$x2)]
lset mt 0 1 [expr 2*($y1-$y2)]
lset mt 1 0 [expr 2*($x1-$x3)]
lset mt 1 1 [expr 2*($y1-$y3)]
set const {{?} {?}}
lset const 0 [expr {(pow($x1,2)+pow($y1,2)-pow($d1,2))-(pow($x2,2)+pow($y2,2)-pow($d2,2))}]
lset const 1 [expr {(pow($x1,2)+pow($y1,2)-pow($d1,2))-(pow($x3,2)+pow($y3,2)-pow($d3,2))}]
set x [expr {double([lindex [Inverse3 $mt] 0 0] * [lindex $const 0]
+ [lindex [Inverse3 $mt] 0 1] * [lindex $const 1])}]
set y [expr {double([lindex [Inverse3 $mt] 1 0] * [lindex $const 0]
+ [lindex [Inverse3 $mt] 1 1] * [lindex $const 1])}]
puts $result "x location of object is: $x \ny location of object is: $y"
}
错误:
ns: processDistances 42 200 41: wrong # args: should be "Inverse2 m"
while executing
"Inverse2 {*} $distances"
(procedure "processDistances" line 32)
invoked from within
"processDistances 42 200 41"
我成功地获得了proc processDistances 的输出,这是一个排序列表,但是当我使用Inverse2 {*}$distances 编写的processDistances 命令(我有tcl8.5)将此输出传递给procedure Inverse2 时。我得到了以上错误。我哪里错了。请帮帮我。
【问题讨论】:
-
让它工作以生成您显示的错误。然后应用此修复程序以使其更进一步。但之后会产生错误,因为 lassign 将变量 x1、y1 设置为空字符串。关键变更:
Inverse2 $distances -
@Ron Norris 我希望
$distances代替proc Inverse2的参数“m”,然后它应该在proc Inverse2中提取$distances和lassign的索引值 -
通过调用
Inverse2 $distances,它将距离列表变量传递给 Inverse2 过程(代替“m”)。这不是你想要的吗?如果你希望它通过引用传递,你也可以这样做。 -
@Ron Norris 对不起,我没有得到你。你的意思是说我应该最后添加一行来调用
Inverse2 $distances??或者proc Inverse2 {m} {我应该写proc Inverse2 {$distances} {(通过在代码中保留Inverse2 {*}$distances)?
标签: tcl parameter-passing proc