如何在tcl中将一个过程的输出作为参数传递给另一个过程

如何在tcl中将一个过程的输出作为参数传递给另一个过程,tcl,parameter-passing,proc,Tcl,Parameter Passing,Proc,我想把一个过程的输出作为一个参数传递给另一个过程,这个过程是一个列表 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)

我想把一个过程的输出作为一个参数传递给另一个过程,这个过程是一个列表

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"

 }

我正在成功地获得
proc processdistance
的输出,这是一个排序列表,但当我使用
Inverse2{*}$distance
命令将此输出传递给
procedure Inverse2
时,我使用了
processdistance
编写的
(我有Tc8.5).我犯了以上错误。我错在哪里了。请帮我解决。

我建议您在我替换它时运行它。如果这不起作用,我不知道你想要什么

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
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"

 }
proc距离{n1 n2 nd1 nd2}{
集合x1[expr int([$n1集合X_uz])]
集合y1[expr int([$n1集合Y])]
集合x2[expr int([$n2集合X_U4;]])]
集合y2[expr int([$n2集合Y])]
集合d[expr hypot($x2-$x1,$y2-$y1)]
返回[列表$nd1$nd2$x1$y1$x2$y2$d]
}
进程距离{计数阈值{过滤器”“}{
全局节点_
设置距离{}
对于{set i 1}{$i<$count}{incr i}{
对于{set j 1}{$j<$count}{incr j}{
#跳过自我比较
如果{$i==$j}继续
#应用目标过滤器
如果{$filter ne”“&&&$j!=$filter}继续
#获取距离信息
设置此距离[距离$node_u($i)$node_($j)$i$j]
#检查节点是否足够靠近
如果{[lindex$thisDistance 6]<$threshold}{
lappend距离$thisDistance
}
}
}
#按距离对这些对进行排序
设置距离[lsort-实际-增加-索引6$距离]
#逆2{*}$距离
反向2$距离
}
$ns在8.5处[列表$val(nn)200 41]
proc Inverse2{m}{
设置结果[打开R.tr w]
lassign[lindex$m 0 2]x1
lassign[lindex$m3]y1
lassign[lindex$m 0 4]d1
lassign[lindex$m 1 2]x2
lassign[lindex$m 1 3]y2
lassign[lindex$m 14]d2
lassign[lindex$m2]x3
lassign[lindex$m 2 3]y3
lassign[lindex$m 2 4]d3
集合mt{{???}{???}
lset mt 0[expr 2*($x1-$x2)]
lset mt 01[expr 2*($y1-$y2)]
lset mt 10[expr 2*($x1-$x3)]
lset mt 1[expr 2*($y1-$y3)]
集合常量{{}{}
lset常量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))]
集合x[expr{double([lindex[Inverse3$mt]0]*[lindex$const 0]
+[lindex[Inverse3$mt]01]*[lindex$const 1])]
集合y[expr{double([lindex[Inverse3$mt]10]*[lindex$const 0]
+[lindex[Inverse3$mt]11]*[lindex$const 1])]
放置$result“对象的x位置为:$x\n对象的任何位置为:$y”
}

让它工作以生成您显示的错误。然后应用此修复程序使其更进一步。但是在这之后会产生错误,因为lassign将变量x1、y1设置为空字符串。关键更改:
Inverse2$distance
@Ron Norris我希望
$distance
取代
proc Inverse2
的参数“m”,然后调用
Inverse2$distance
,提取
$distance
$distance
lassign
的索引值,它将距离列表变量传递给Inverse2过程(代替“m”)。这不是你想要的吗?如果你想让它通过引用传递,你也可以这样做。@Ron Norris很抱歉,我没有得到你。你的意思是说我应该在最后添加行来调用
Inverse2$distance
?,或者相反地
proc Inverse2{m}{
我应该写
proc Inverse2{$distance}{
(通过在代码中保留
Inverse2{*}$distance
),这通常有助于在不剪切粘贴代码中所有不相关的部分的情况下精确定位您的更改。
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
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"

 }