CKCK
CKCK

Reputation: 125

What is wrong with this statement

        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

Upvotes: 3

Views: 6520

Answers (3)

glenn jackman
glenn jackman

Reputation: 246847

I your big error traceback, I see:

if [catch "$self cmd $args" ret] {

Do you really have an instance method named "cmd" (or whatever terminology for the OO system you appear to use)

Do you want:

if {[catch {$self $cmd $args} ret]} { ...

Upvotes: 1

Donal Fellows
Donal Fellows

Reputation: 137587

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.)

Upvotes: 7

bmk
bmk

Reputation: 14147

If you do it as you do you are trying to get the contents of the (non existent variable) tcp and the content of the variable count. What you want is to get the content of tcp$count. You can achieve this using the set command. Simply do:

[set tcp$count] set fid_ $count

Upvotes: 0

Related Questions