2017-07-08 3 views
0

リストである一つのプロシージャの出力を別のプロシージャの引数として渡したいです。私は試したコードです。1つのプロシージャの出力を引数として別のプロシージャに渡す方法

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を持っています)私は間違っています。私はどこか間違っています。私を助けてください。

+0

あなたが示したエラーを生成するようになっています。その後、この修正を適用してさらに進化させました。しかし、lassignsは空の文字列に変数x1、y1を設定しているので、それ以降はエラーが発生します。キーの変更: 'Inverse2 $ distance' –

+0

@Ron Norris' proc Inverse2'の引数である "m"の代わりに '$ distance 'を入れてから、' $ distance'と 'lassign'のインデックス値を抽出する必要があります。 'proc Inverse2' –

+0

' Inverse2 $ distanceances'を呼び出すことによって、distanceリスト変数をInverse2手続き( "m"の代わりに)に渡します。あなたが望んでいたものではありませんか?あなたが参照渡ししたい場合は、それも行うことができます。 –

答えて

0

私はそれを置き換えたときと同じように実行することをお勧めします。これがうまくいかなければ、私はあなたが何を求めているのか分からない。

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" 

} 
+0

通常、コードの無関係な部分をすべて削除することなく変更を特定すると便利です。 –

関連する問題