Debugging 无法更改变量的值 我使用的是一个用TCL和C++构建的NS-2离散事件模拟器。我试图用TCL编写一些代码: set ns [new Simulator] set state 0 $ns at 0.0 "puts \"At 0.0 value of state is: $state\"" $ns at 1.0 "changeVal" $ns at 2.0 "puts \"At 2.0 values of state is: $state\"" proc changeVal {} { global state global ns $ns at-now "set state [expr $state+1]" puts "Changed value of state to $state" } $ns run

Debugging 无法更改变量的值 我使用的是一个用TCL和C++构建的NS-2离散事件模拟器。我试图用TCL编写一些代码: set ns [new Simulator] set state 0 $ns at 0.0 "puts \"At 0.0 value of state is: $state\"" $ns at 1.0 "changeVal" $ns at 2.0 "puts \"At 2.0 values of state is: $state\"" proc changeVal {} { global state global ns $ns at-now "set state [expr $state+1]" puts "Changed value of state to $state" } $ns run,debugging,tcl,simulation,ns2,otcl,Debugging,Tcl,Simulation,Ns2,Otcl,以下是输出: At 0.0 value of state is: 0 Changed value of state to 0 At 2.0 values of state is: 0 国家的价值似乎没有改变。我不确定我在使用TCL时是否做错了什么。有人知道这里可能出了什么问题吗 编辑:谢谢你的帮助。实际上,ns-2是我无法控制的东西(除非我重新编译模拟器本身)。我尝试了这些建议,结果如下: At 0.0 value of state is: 0 Changed value of state t

以下是输出:

At 0.0 value of state is: 0
Changed value of state to 0
At 2.0 values of state is: 0
国家的价值似乎没有改变。我不确定我在使用TCL时是否做错了什么。有人知道这里可能出了什么问题吗

编辑:谢谢你的帮助。实际上,ns-2是我无法控制的东西(除非我重新编译模拟器本身)。我尝试了这些建议,结果如下:

At 0.0 value of state is: 0
Changed value of state to 0
At 2.0 values of state is: 0
守则:

set ns [new Simulator]

set state 0

$ns at 0.0 "puts \"At 0.0 value of state is: $state\""
$ns at 1.0 "changeVal"
$ns at 9.0 "puts \"At 2.0 values of state is: $state\""

proc changeVal {} {
    global ns
    set ::state [expr {$::state+1}]
    $ns at-now "puts \"At [$ns now] changed value of state to $::state\""
}

$ns run
set ns [new Simulator]

set state 0

$ns at 0.0 "puts \"At 0.0 value of state is: $state\""
$ns at 1.0 "changeVal"
$ns at 9.0 "puts \"At 2.0 values of state is: $state\""

proc changeVal {} {
    global ns
    set ::state [expr {$::state+1}]
    $ns at 1.0 {puts "At 1.0 values of state is: $::state"}
}

$ns run
输出为:

At 0.0 value of state is: 0
At 1 changed value of state to 1
At 2.0 values of state is: 0
At 0.0 value of state is: 0
At 1.0 values of state is: 1
At 2.0 values of state is: 0
至于守则:

set ns [new Simulator]

set state 0

$ns at 0.0 "puts \"At 0.0 value of state is: $state\""
$ns at 1.0 "changeVal"
$ns at 9.0 "puts \"At 2.0 values of state is: $state\""

proc changeVal {} {
    global ns
    set ::state [expr {$::state+1}]
    $ns at-now "puts \"At [$ns now] changed value of state to $::state\""
}

$ns run
set ns [new Simulator]

set state 0

$ns at 0.0 "puts \"At 0.0 value of state is: $state\""
$ns at 1.0 "changeVal"
$ns at 9.0 "puts \"At 2.0 values of state is: $state\""

proc changeVal {} {
    global ns
    set ::state [expr {$::state+1}]
    $ns at 1.0 {puts "At 1.0 values of state is: $::state"}
}

$ns run
输出为:

At 0.0 value of state is: 0
At 1 changed value of state to 1
At 2.0 values of state is: 0
At 0.0 value of state is: 0
At 1.0 values of state is: 1
At 2.0 values of state is: 0
似乎不起作用。。。不确定是ns2还是我的代码有问题…

编辑:现在了解状态机

首先,您使用的引用语法会给您带来麻烦。您通常应该使用list构建Tcl命令,这可以确保

您当前的
调用
在您进行调用时正在替换
状态
变量(即,当值不变且为0时)。您需要的是:

$ns at-now 0.0 {puts "At 0.0 value of state is: $::state"}
$ns at-now 2.0 {puts "At 2.0 value of state is: $::state"}
看起来您的
changeVal
写得很好(第一个版本有一些相同的替换问题),并且您传入了将在本地使用的变量引用,因此没有设置全局状态

问题第一版的部分解决方案-使用全局引用,并引用
[
$
以防止在调用时替换:

$ns at-now "set ::state \[expr {\$::state + 1}\]"
或者,使用大括号:

$ns at-now {set ::state [expr {$::state + 1}]}
编辑:现在了解状态机

首先,您使用的引用语法会给您带来麻烦。您通常应该使用list构建Tcl命令,这可以确保

您当前的
调用
在您进行调用时正在替换
状态
变量(即,当值不变且为0时)。您需要的是:

$ns at-now 0.0 {puts "At 0.0 value of state is: $::state"}
$ns at-now 2.0 {puts "At 2.0 value of state is: $::state"}
看起来您的
changeVal
写得很好(第一个版本有一些相同的替换问题),并且您传入了将在本地使用的变量引用,因此没有设置全局状态

问题第一版的部分解决方案-使用全局引用,并引用
[
$
以防止在调用时替换:

$ns at-now "set ::state \[expr {\$::state + 1}\]"
或者,使用大括号:

$ns at-now {set ::state [expr {$::state + 1}]}

问题是您正在立即替换变量的值,而不是在计算代码时。您需要推迟替换。因此,不是:

$ns at 2.0 "puts \"At 2.0 values of state is: $state\""
这样做:

$ns at 2.0 {puts "At 2.0 values of state is: $state"}
在进行这样的调用时,最好将比简单的命令调用更复杂的内容放在过程中,而不进行替换。使其正常工作要容易得多

[编辑]

另外,
at now
仍在延迟执行其主体,直到当前
at
返回之后。

问题是您正在立即替换变量的值,而不是在计算代码时。您需要延迟替换。因此,而不是:

$ns at 2.0 "puts \"At 2.0 values of state is: $state\""
这样做:

$ns at 2.0 {puts "At 2.0 values of state is: $state"}
在进行这样的调用时,最好将比简单的命令调用更复杂的内容放在过程中,而不进行替换。使其正常工作要容易得多

[编辑]

另外,现在的
仍在推迟执行其正文,直到当前的
at
返回。

我不知道为什么这样做有效,但它有效:

set ns [new Simulator]

set state 0

proc changeVal {} {
    global ns
    incr ::state
    $ns at-now {puts "Local::At [$ns now] values of state is: $::state"}
}

$ns at 0.0 "puts \"Global::At 0.0 value of state is: $state\""
changeVal
$ns at 9.0 "puts \"Global::At 2.0 values of state is: $state\""

$ns run
输出:

Global::At 0.0 value of state is: 0
Local::At 0 values of state is: 1
Global::At 2.0 values of state is: 1

如果有人知道一个解释,那就太好了。

我不知道为什么这样做有效,但它有效:

set ns [new Simulator]

set state 0

proc changeVal {} {
    global ns
    incr ::state
    $ns at-now {puts "Local::At [$ns now] values of state is: $::state"}
}

$ns at 0.0 "puts \"Global::At 0.0 value of state is: $state\""
changeVal
$ns at 9.0 "puts \"Global::At 2.0 values of state is: $state\""

$ns run
输出:

Global::At 0.0 value of state is: 0
Local::At 0 values of state is: 1
Global::At 2.0 values of state is: 1


如果有人知道一个解释,那就太好了。

你考虑过
incr state
而不是
set state[expr{$state+1}]
?是的。只是尝试了一下……似乎不起作用。你应该考虑使用全局引用
incr state
而不是
set state[expr{$state+1}]
?是的。只是尝试了一下……似乎不起作用。它应该与全局参考感谢一起寻找答案。由于ns-2是一个模拟器,我必须重新编译它以合并您建议的更改。我会处理它。至于其他事情,我刚刚更新了我的帖子。谢谢您的时间。@Legend,我更新了它以提供更清晰的答案(b/c我重新仔细阅读了问题)。有两个问题需要克服,我只是回答了不太重要的一个。这个答案应该可以解决您的问题,而无需更改ns-2。谢谢Trey…对于任何有兴趣了解正确代码的人,请在此处查找我的答复。如果您想逃避一切,不妨将其放入{}取而代之的是,谢谢你的回答。由于ns-2是一个模拟器,我必须重新编译它,以纳入你建议的更改。我会处理它。至于其他事情,我刚刚更新了我的帖子。谢谢你的时间。@Legend,我更新了以提供更清晰的答案(b/c我重新仔细阅读了问题)。有两个问题需要克服,我只是回答了不太重要的一个。这个答案应该可以解决您的问题,而无需更改ns-2。谢谢Trey…对于任何有兴趣了解正确代码的人,请在此处查找我的答复。如果您想逃避一切,不妨将其放入{}相反,谢谢。用输出更新了我的帖子。似乎不是问题。谢谢。用输出更新了我的帖子。似乎不是问题。请参阅我的答案了解其工作原理(提示:您更改了命令的顺序,这影响了被替换的内容)。请参阅我的答案了解其工作原理(提示:您更改了命令的顺序,这会影响被替换的内容)。