Question

i am using 20 node in ns2 , i am trying to access the a(0) a(1) a(2) a(3) a(4) variables with for loop how can i do that

here is my code

for {set i 0} {$i < $val(nn)} {incr i} {
    set sink($i) [new Agent/LossMonitor]
    $ns attach-agent $n($i) $sink($i)

}

but its gives an error bad variable name "sink(0)": upvar won't create a scalar variable that looks like an array element

i declare the variable in following manner

proc record {} {
  global sink(0) sink(1) sink(2) sink(3) sink(4) sink(5)
}
Was it helpful?

Solution

Just use

global sink

(global is just a special upvar case)

variables that end with (...) are (associative) arrays, using numbers as key is not recommended, it is better to use a list instead, e.g.

set mylist {}
# append some elements
lappend mylist "foo" "bar" "baz"
# get the 2nd element
puts [lindex $mylist 1]
# set the 3rd element to "Hello World"
lset mylist 2 "Hello World"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top