tcl脚本学习六:switch的学习

lesson 6 :switch 的用法


;# Set the variables we'll be comparing
set x "ONE";
set y 1;
set z "ONE";
//例子的基本数值


1. switch $x
"ONE" "puts ONE=1"
"TWO" "puts TWO=2"
"default" "puts NO_MATCH";
//switch 的基本用法


2. 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";}
//强调!! 这里的{}不再具有之前puts里面的原封不动输出括号内的意思,而是将该条件下的成立后的条件包括起来

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

3.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 is NOT A MATCH"}
}

原文地址:https://www.cnblogs.com/gold-life/p/5731126.html