Sorting 无法在tcl中对列表进行排序

Sorting 无法在tcl中对列表进行排序,sorting,tcl,ns2,Sorting,Tcl,Ns2,我在NS2中有下面的代码,它计算两个节点之间的距离,并将其放在列表“nbr”中。我想根据值“d”按升序对列表进行排序,并再次将其存储在列表中,以便进一步使用我使用的lsort命令,但它给出的列表与未排序的列表相同。 请帮忙 代码: 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

我在NS2中有下面的代码,它计算两个节点之间的距离,并将其放在列表“nbr”中。我想根据值“d”按升序对列表进行排序,并再次将其存储在列表中,以便进一步使用我使用的
lsort
命令,但它给出的列表与未排序的列表相同。 请帮忙

代码:

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 int(sqrt(pow(($x2-$x1),2)+pow(($y2-$y1),2)))]
    if {$d<300} {
        if {$nd2!=$nd1 && $nd2 == 11} {
            set nbr "{$nd1 $nd2 $x1 $y1 $d}"
            set m [lsort -increasing -index 4 $nbr]
            puts $m
        }
    }
}

for {set i 1} {$i < $val(nn)} {incr i} {
    for {set j 1} {$j < $val(nn)} {incr j} {
        $ns at 5.5 "distance $node_($i) $node_($j) $i $j"
    }
}

现在,您正在分别计算每个距离,但实际上并没有将它们全部收集到一个可以排序的列表中

我们先重写
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 int(sqrt(pow(($x2-$x1),2)+pow(($y2-$y1),2)))]
    # Why not: set d [expr hypot($x2-$x1,$y2-$y1)]

    # I'm keeping *everything* we know at this point
    return [list $nd1 $nd2 $n1 $n2 $d $x1 $y1 $x2 $y2]
}
然后,我们需要另一个过程来处理整个集合(在模拟器调用它时)并进行排序。它将调用
distance
来获取单个记录,因为我们已经将该信息分解了

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 4] < $threshold} {
                lappend distances $thisDistance
            }
        }
    }

    # Sort the pairs, by distances
    set distances [lsort -real -increasing -index 4 $distances]

    # Print the sorted list
    foreach tuple $distances {
        puts "{$tuple}"
    }
}

非常感谢您的帮助。我还有一个问题。我想在过程之外访问此排序列表。就像我必须
lassign[lindex$sortedfile 0 2]a
。我应该调用哪个文件来代替“$sortedfile”,我尝试使用距离文件,即使用全局变量的过程之外的排序列表。我在
设置距离
之后在过程内部又创建了一个变量,使其成为全局变量,并将距离文件分配给该变量。但当我在过程外部使用该文件时,会出现错误
无法读取“文件”:没有此类变量
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 4] < $threshold} {
                lappend distances $thisDistance
            }
        }
    }

    # Sort the pairs, by distances
    set distances [lsort -real -increasing -index 4 $distances]

    # Print the sorted list
    foreach tuple $distances {
        puts "{$tuple}"
    }
}
# We recommend building callbacks using [list], not double quotes
$ns at 5.5 [list processDistances $val(nn) 300 11]