2011-04-18 29 views
2
 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 

    } 

我的問題是與線#$tcp$count set fid_ $count 當我試着執行它時,它說有什麼不對本聲明

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

它說無法讀取TCP,以及它不應該閱讀TCP應該讀它在第一次迭代中爲tcp0,在第二次中爲tcp1,依此類推。 什麼是我做錯了?

感謝

編輯:

我使用數組,謝謝你的提示嘗試。它的工作的大部分地區,但在一個特定的情況下 當我這樣做:

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

它給了我下面的錯誤:

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) 

我知道它很長,但我會很感激你幫助

回答

0

如果你這樣做,你試圖獲取(不存在的變量)tcp的內容和變量count的內容。你想要的是獲得tcp$count的內容。您可以使用set命令來實現此目的。簡單地做:

[set tcp$count] set fid_ $count 
+0

雖然,數組是一個更易讀的選擇:'$ tcp($ count)' – 2011-04-18 10:50:23

+0

@Bryan:當然可以。我應該提到這一點。 – bmk 2011-04-18 12:15:40

6

的問題是,變量名的$後,解析在第一個非字母數字字符停止(除了我將在稍後提起的情況下)。這意味着$tcp$count被解釋爲變量tcpcount變量(僅限您定義的變量之一)的串聯字符串。

處理這個問題的最好方法是使用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 
} 

(是變量訪問語法處理特殊情況;它開始處理一個關聯數組訪問(它繼續匹配),假設沒有未加引號的空格)。

(變量名中的其他特殊情況是::,這是Tcl的命名空間分隔符)

+0

用於說明,+ 100用於使用關聯數組的建議。 Tcl可以讓你用其他變量編寫複雜的變量名,這很酷,但很少使用可讀的代碼。 – 2011-04-18 10:53:57

+0

這引發了另一個錯誤,請參閱原始帖子的編輯。任何幫助深表感謝。 – CKCK 2011-04-18 11:48:19

+0

@Chanditha:這是一個單獨的問題。當你將這些問題作爲多個問題提出時,堆棧溢出效果最佳。 (我不知道答案是什麼,錯誤似乎在圖書館內部很深。) – 2011-04-18 19:54:20

0

我你的大錯誤追蹤,我看到:

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

你真的有命名實例方法「CMD」(或其他術語的OO系統,您似乎使用)

你想:

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