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

How to pass output of one procedure as an argument to another procedure in tcl

我想将一个过程的输出作为参数传递给另一个 procedure.Below 是我试过的代码。

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 的输出,这是一个排序列表,但是当我使用 processDistances 中编写的命令 Inverse2 {*}$distances 将此输出传递给 procedure Inverse2 时(我有 tcl8.5)。我已经超过 error.Where 我很笨 wrong.please 帮帮我。

我建议你 运行 就像我替换它一样。如果这不起作用,我不确定你要求什么。

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"

 }