我试图运行以下tcl脚本,但在过程thpt_rec中遇到错误
ns:thpt_rec:无法读取"tcps(0)":没有这样的变量
执行
时"$tcps(0)设置字节"
(程序"thpt_rec"第4行)
从
内部调用"thpt_rec"
但是,当我在所有地方用一个名为sink的变量替换这个tcps(0)时,一切都很好。请阐明这个问题。
set ns [new Simulator]
set tracefile [open tcpf1.tr w]
set thptfile [open thpt.tr w]
$ns trace-all $tracefile
proc finish {} {
global ns tracefile thptfile
$ns flush-trace
close $tracefile
close $thptfile
exit 0
}
proc thpt_rec {} {
global ns tcps(0) thptfile
set time 1.0
set bw [$tcps(0) set bytes_]
set now [$ns now]
puts $thptfile "$now [expr $bw/$time*8/1000000]"
$tcps(0) set bytes_ 0
$ns at [expr $now+$time] "thpt_rec"
}
for {set i 0} {$i < 10} {incr i} {
set n($i) [$ns node]
}
for {set i 0} {$i < 4} {incr i} {
$ns duplex-link $n($i) $n(4) 10Mb 2ms DropTail
$ns duplex-link $n(5) $n([expr ($i+6)]) 10Mb 2ms DropTail
}
$ns duplex-link $n(4) $n(5) 0.25Mb 10ms DropTail
set tcp(0) [new Agent/TCP]
$ns attach-agent $n(0) $tcp(0)
set tcps(0) [new Agent/TCPSink]
$ns attach-agent $n(6) $tcps(0)
$ns connect $tcp(0) $tcps(0)
set ftp(0) [new Application/FTP]
$ftp(0) attach-agent $tcp(0)
$ns at 0.0 "thpt_rec"
for {set i 0} {$i < 1} {incr i} {
$ns at [expr (5.0 * $i )] "$ftp($i) start"
$ns at 140.0 "$ftp($i) stop"
}
$ns at 150.0 "finish"
$ns run
更换
global ns tcps(0) thptfile
带有
global ns tcps thptfile
它应该可以正常工作。
问题是 因此,如果要在过程中访问全局数组的元素,则应将数组变量本身声明为tcps
变量是一个数组,数组元素不是变量—它们只是价值观global
。