Unable to change the value of the variable
Posted
by Legend
on Stack Overflow
See other posts from Stack Overflow
or by Legend
Published on 2010-03-29T14:12:44Z
Indexed on
2010/03/29
15:53 UTC
Read the original article
Hit count: 565
I'm using a discrete event simulator called ns-2 that was built using Tcl and C++. I was trying to write some code in 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
Here's the output:
At 0.0 value of state is: 0
Changed value of state to 0
At 2.0 values of state is: 0
The value of state does not seem to change. I am not sure if I am doing something wrong in using TCL. Anyone has an idea as to what might be going wrong here?
EDIT: Thanks for the help. Actually, ns-2 is something over which I do not have much control (unless I recompile the simulator itself). I tried out the suggestions and here's the output:
for the code:
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
the output is:
At 0.0 value of state is: 0
At 1 changed value of state to 1
At 2.0 values of state is: 0
And for the code:
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
the output is:
At 0.0 value of state is: 0
At 1.0 values of state is: 1
At 2.0 values of state is: 0
Doesn't seem to work... Not sure if its a problem with ns2 or my code...
© Stack Overflow or respective owner