Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1441953
  • 博文数量: 244
  • 博客积分: 3353
  • 博客等级: 中校
  • 技术积分: 3270
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-09 17:56
文章分类

全部博文(244)

文章存档

2023年(7)

2022年(7)

2021年(4)

2020年(1)

2019年(2)

2017年(2)

2016年(3)

2015年(11)

2014年(20)

2013年(10)

2012年(176)

分类: WINDOWS

2012-06-05 23:13:57

1 switch的分支中的命令使用花括号包含,但是并不会影响花括号中的命令执行,切记,这是switch的格式;

2 如果不想分支条件进行置换,需要在外加上花括号,不会影响分支中的命令执行

 例子:008_switch.tcl

;# Set the variables we'll be comparing

set x "ONE";

set y 1;

set z "ONE";

 

;# This is legal

switch $x "ONE" "puts ONE=1" "TWO" "puts TWO=2" "default" "puts NO_MATCH"  ;#这种写法合法,但是阅读不便

 

switch $x \

  "ONE" "puts ONE=1"  \

  "TWO" "puts TWO=2" \

  "default" "puts NO_MATCH"               ;#这种写法好看一些,推荐

 ;#下面这种写法$z被置换,走入$z的条件分支,表面上看条件分支中的命令在花括号内,这只是switch的一种格式,所以其中的命令仍然被执行了。

switch $x \

  "$z"      {set y1 [expr $y+1]; puts "MATCH \$z. $y + $z is $y1" } \

  "ONE"     {set y1 [expr $y+1]; puts "MATCH ONE. $y + one is $y1"} \

  "TWO"     {set y1 [expr $y+2]; puts "MATCH TWO. $y + two is $y1" } \

  "THREE"   {set y1 [expr $y+3]; puts "MATCH THREE. $y + three is $y1" } \

  "default" {puts "$x does not match any of these choices"}

 

;# This form of the command disables variable substitution in the pattern

;#下面为了不置换$z,在外层加上了花括号,于是走入了ONE分支,而分支中的命令仍然被执行了

package require Tcl
package require Tk

set x ONE
set y 5
switch $x {

  "TWO"      {puts TWO=2}
  "default"  {puts NO_MATCH}
   "ONE"     {set x wewewe;puts $x}
}

阅读(1843) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~