1. 变量和变量替换
例1-1
set foo "jhon"
puts "my name is $foo"
例1-2
set month 3
set day 2
set year 79
set date "$month:$day:$year"
puts $date
例1-3
set foo "puts hi"
eval $foo
2.表达式
例2-1
set value [expr 0==1]
puts $value
例2-2
set value [expr 2>=1]
puts $value
例2-3
set value [expr 2+3]
puts $value
3.指令替换
例3-1
puts "I am [expr 10*2] years old, and my IQ is [expr 100-25]"
例3-2
set my_heigth 6.0
puts "If I was 2 inchs taller,I would be [expr $my_heigth + (2.0/12.0) ] feet tall"
4.流程控制
例4-1
set my_planet "earth"
if {$my_planet=="earth"}{
puts "I feel rigth at home ."
}elseif{$my_planet == "venus"}{
puts "This is ont my home."
}else {
puts "I am neither from earth,nor from Venus."
}
set temp 95
if{$temp < 80}{
puts "It's a little chilly."
}else {
puts "Warm enough fo me."
}
例4-2
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."}
}
例4-3
for {set i 0} {$i<5} {incr i 1} {
puts "In the for loop,and i == $i"
}
例4-4
set i 0
while {$i<5}{
puts "In the while loop,and i == $i"
incr i 1
}
例4-5
foreach vowel{a e i o u}{
puts "$vowel is a vowel"
}