第2章_Tcl与OTcl___NS
Tcl和OTcl
第 2 章
Tcl命令格式
变量
组合和替代
数学运算
过程
流程控制命令
注释
数组
OTer from Earth, nor from Venus."
}
set temp 95
if {$temp < 80} {
puts "It's a little chilly."
} else {
puts "Warm enough for me."
}
執行方法:
ns
執行的結果:
I feel right at home.
Warm enough for me.
switch
Example:()
set num_legs 4
switch $num_legs {
2 {puts "It could be a human."}
4 {puts "It could be a cow."}
6 {puts "It could be an ant."}
8 {puts "It could be a spider."}
default {puts "It could be anything."}
}
执行方法:
ns
执行結果:
It could be a cow.
for
Example :()
for {set i 0} {$i < 5} {incr i 1} {
puts "In the for loop, and i = $i"
}
执行方法:
ns
执行結果:
In the for loop, and i == 0
In the for loop, and i == 1
In the for loop, and i == 2
In the for loop, and i == 3
In the for loop, and i == 4
while
Example : ()
set i 0
while {$i < 5} {
puts "In the while loop, and i == $i"
incr i 1
}
执行方法:
ns
执行結果:
In the while loop, and i == 0
In the while loop, and i == 1
In the while loop, and i == 2
In the while loop, and i == 3
In the while loop, and i == 4
foreach
Example:()
foreach vowel {a e i o u} {
puts "$vowel is a vowel"
}
执行方法:
ns
执行結果:
a is a vowel
e is a vowel
i is a vowel
o is a vowel
u is a vowel
注释
;#
注释一天语句的后半部分
set rate ;利息
#
在语句前加#,表示注释整行
#银行参数
多行注释用“\”
#it’s a fine day\gentle breeze;sunny
数组
Example :()
set myarray(0) "Zero"
set myarray(1) "One"
set myarray(2) "Two"
for {set i 0} {$i < 3} {incr i 1} {
puts $myarray($i)
}
执行方法:
ns
执行结果:
Zero
One
Two
Example: ()
set person_info(name) "Fred Smith“
set person_info(age) "25"
set person_info(occupation) "Plumber"
foreach thing {name age occupation}
{
puts "$thing == $person_info($thing)"
}
执行方法
第2章 Tcl与OTcl NS 来自淘豆网m.daumloan.com转载请标明出处.