Não é possível alterar o valor da variável
-
22-09-2019 - |
Pergunta
Estou usando um simulador de eventos discretos chamado ns-2 que foi construído usando Tcl e C++.Eu estava tentando escrever algum código em 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
Aqui está o resultado:
At 0.0 value of state is: 0
Changed value of state to 0
At 2.0 values of state is: 0
O valor do estado não parece mudar.Não tenho certeza se estou fazendo algo errado ao usar o TCL.Alguém tem uma idéia do que pode estar errado aqui?
EDITAR:Obrigado pela ajuda.Na verdade, o ns-2 é algo sobre o qual não tenho muito controle (a menos que eu recompile o próprio simulador).Eu experimentei as sugestões e aqui está o resultado:
para o código:
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
a saída é:
At 0.0 value of state is: 0
At 1 changed value of state to 1
At 2.0 values of state is: 0
E para o código:
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
a saída é:
At 0.0 value of state is: 0
At 1.0 values of state is: 1
At 2.0 values of state is: 0
Parece que não funciona...Não tenho certeza se é um problema com o ns2 ou com o meu código ...
Solução
Como Supriyo mencionado, este é um tópico muito amplo.No entanto, a Microsoft documentou o planejamento para o gerenciamento de documentos em grandes detalhes.Comece aqui: http://technet.microsoft.com/en-us/library/cc263266(v=office.14).aspx
Outras dicas
O problema é que você está substituindo o valor das suas variáveis imediatamente, e não no momento em que o código é avaliado.Você precisa adiar a substituição.Assim, em vez de:
$ns at 2.0 "puts \"At 2.0 values of state is: $state\""
Fazem isto:
$ns at 2.0 {puts "At 2.0 values of state is: $state"}
É uma boa prática colocar algo mais complexo do que uma simples chamada de comando sem substituição em um procedimento ao fazer uma chamada como esta.Muito mais fácil fazer funcionar direito.
[EDITAR]
Também o at-now
ainda está adiando fazer seu corpo até depois do atual at
retorna.
Não sei por que isso funciona, mas funciona:
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
Resultado:
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
Se alguém souber uma explicação, isso seria ótimo.