Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with this statement

Tags:

tcl

        for {set count 0} {$count<$num_of_UEs} { incr count } {
            puts $count
            set tcp$count [new Agent/TCP]
            #$tcp$count set fid_ $count
            #$tcp$count set prio_ 2

    }

My problem is with the line#$tcp$count set fid_ $count When I try to execute it it says

 can't read "tcp": no such variable
    while executing
"$tcp$count set fid_ $count"
    ("for" body line 4)
    invoked from within
"for {set count 0} {$count<$num_of_UEs} { incr count } {
                                puts $count
                                set tcp$count [new Agent/TCP]
                                $tcp$count set fid_ $count
                                $tcp$coun..."

It says Can't read tcp, well it shouldnt read tcp it should read it as tcp0 in the first iteration and tcp1 in the second and so on. What amI doing wrong?

thanks

EDIT:

I tried using arrays, thanks for the TIP. It worked for most parts but in one specific case when I did this:

    for {set count 0} {$count<$num_of_UEs} { incr count } {
    set ftp($count) [new Application/FTP]
    $ftp($count) attach-agent $tcp($count)
    }

It gives me the following error:

Came here 0

    (_o180 cmd line 1)
    invoked from within
"_o180 cmd target _o99"
    invoked from within
"catch "$self cmd $args" ret"
    invoked from within
"if [catch "$self cmd $args" ret] {
set cls [$self info class]
global errorInfo
set savedInfo $errorInfo
error "error when calling class $cls: $args" $..."
    (procedure "_o180" line 2)
    (SplitObject unknown line 2)
    invoked from within
"$agent target [[$self node] entry]"
    (procedure "_o98" line 2)
    (RtModule attach line 2)
    invoked from within
"$m attach $agent $port"
    (procedure "_o97" line 4)
    (Node add-target line 4)
    invoked from within
"$self add-target $agent $port"
    (procedure "_o97" line 15)
    (Node attach line 15)
    invoked from within
"$node attach $agent"
    (procedure "_o3" line 2)
    (Simulator attach-agent line 2)
    invoked from within
"$ns attach-agent $node2 $tcp($count)"
    ("for" body line 3)
    invoked from within
"for {set count 0} {$count<$num_of_UEs} { incr count } {
                puts "Came here $count"
                $ns attach-agent $node2 $tcp($count)
                }"
    (file "hsexample.tcl" line 104)

I know its a long one, but I would really appreciate your help

like image 537
CKCK Avatar asked Dec 08 '25 23:12

CKCK


1 Answers

The problem is that the parsing of the variable name after the $ stops at the first non-alphanumeric character (except for cases that I'll mention in a moment). This means that $tcp$count is interpreted as the string that is the concatenation of the contents of the tcp variable and the count variable (only one of which you've defined).

The best way of dealing with this is to use Tcl's associative arrays:

for {set count 0} {$count<$num_of_UEs} { incr count } {
    puts $count
    set tcp($count) [new Agent/TCP]
    $tcp($count) set fid_ $count
    $tcp($count) set prio_ 2
}

The ( is a special case in variable access syntax handling; it starts processing an associative array access (which goes on to the matching ), assuming no unquoted spaces).

(The other special case in variable names is ::, which is Tcl's namespace separator.)

like image 112
Donal Fellows Avatar answered Dec 12 '25 06:12

Donal Fellows



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!