Chinaunix首页 | 论坛 | 博客
  • 博客访问: 182548
  • 博文数量: 42
  • 博客积分: 2235
  • 博客等级: 大尉
  • 技术积分: 460
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-15 20:17
文章分类

全部博文(42)

文章存档

2012年(1)

2011年(7)

2010年(33)

2009年(1)

我的朋友

分类:

2010-02-04 21:05:23

if -------按条件执行脚本

语法:
if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN?

通过解析expr1(表达式)取值来执行后续脚本。表达式的值必须为一个布尔值(对于数字值,0为false,其他为true;对于字符串值,true或者yes为true,false或者no为false),为真则执行后续脚本,为假则执行elseif后续脚本,依次类推。

举例:
if {$vbl == 1} { puts "vbl is one" }

With an else-clause:

if {$vbl == 1} {
   puts "vbl is one"
} else {
   puts "vbl is not one"
}

With an elseif-clause too:

if {$vbl == 1} {
   puts "vbl is one"
} elseif {$vbl == 2} {
   puts "vbl is two"
} else {
   puts "vbl is not one or two"
}

if {
   $vbl == 1 || $vbl == 2 || $vbl == 3
} then {
   puts "vbl is one, two or three"
}

for ----- for循环

语法:
for start test next body

该命令和c 的for类似。start,next,body必须为tcl命令字符串,test为一个表达式字符串。先执行start,接着重复test,如果结果为真则执行body后在执行nex

举例:
for {set x 0} {$x<10} {incr x} {
   puts "x is $x"
}

for {set x 0} $x<10 {incr x} {
   puts "x is $x"
}

for {set x 1} {$x<=1024} {set x [expr {$x * 2}]} {
   puts "x is $x"
}

continue ----跳过当前循环进入下一次循环

语法:
continue

该命令出现在for ,foreach或者while中,其返回TCL_CONTINUE码并导致产生continue异常出现,从而导致跳出当前操作继续下一个循环操作。

举例:
for {set x 0} {$x<10} {incr x} {
   if {$x == 5} {
      continue
   }
   puts "x is $x"
}

break ----跳出循环

语法:
break

该命令出现在for ,foreach或者while中,其返回TCL_BREAK码并导致break异常出现。该异常导致中断整个循环并返回正常值。

举例:
for {set x 0} {$x<10} {incr x} {
   if {$x > 5} {
      break
   }
   puts "x is $x"
}

while ---- 在满足条件的情况下重复执行后续脚本

语法:
while test body

while首先解析test(表达式),为真则执行后续脚本。test应该总是由{}包括,否则在while执行之前会先进行变量替换,会导致test中的变量值会忽略body中对其的修改,会导致无穷循环。如果test由{}包括起来,变量替换将会延迟直到test表达式整体被解析,因此变量的变化是可见的。

举例:
set x 0
while {$x<10} {
    puts "x is $x"
    incr x
}


set lineCount 0
while {[gets $chan line] >= 0} {
    puts "[incr lineCount]: $line"
}

foreach ---- 在一个或多个列表迭代所有元素

语法:
foreach varname list body
foreach varlist1 list1 ?varlist2 list2 ...? body

该命令实现取自一个或者多个列表的循环变量执行循环操作。有两种方式:1)varname作为一个循环变量,依次遍历list中的所有取值并在body中执行;2)变量varlist1依次遍历list1进行取值,varlist2依次遍历list2取值并在body中执行

举例:
set x {}
foreach {i j} {a b c d e f} {
    lappend x $j $i
}
# The value of x is "b a d c f e"
# There are 3 iterations of the loop.

The next loop uses i and j to iterate over two lists in parallel.

set x {}
foreach i {a b c} j {d e f g} {
    lappend x $i $j
}
# The value of x is "a d b e c f {} g"
# There are 4 iterations of the loop.

The two forms are combined in the following example.

set x {}
foreach i {a b c} {j k} {d e f g} {
    lappend x $i $j $k
}
# The value of x is "a d e b f g c {} {}"
# There are 3 iterations of the loop.
 



switch ----根据给定值,解析多个脚本其中之一

语法:
switch ?options? string pattern body ?pattern body ...?
switch ?options? string {pattern body ?pattern body ...?}

switch命令根据string pattern匹配body中的pattern,匹配即进行相关的操作并返回值,先匹配先执行,如果body中最后一个pattern为default则将匹配任何string pattern,没有匹配则返回空
switch 支持的option:
-exact
    精确匹配string pattern 和 pattern,此为默认形式

-glob
    匹配string pattern 和 pattern时使用glob-style匹配

-regexp
    匹配string pattern和pattern时使用正则表达式

- -
    标记option的结束. 其后的参数将被当做string对待

举例:
set foo "abc"
switch abc a - b {expr 1} $foo {expr 2} default {expr 3}

switch -glob aaab {
   a*b     -
   b       {expr 1}
   a*      {expr 2}
   default {expr 3}
}

switch xyz {
   a  -
   b {
      # Correct Comment Placement
      expr 1
   }
   c {
      expr 2
   }
   default {
      expr 3
   }
}
阅读(786) | 评论(0) | 转发(0) |
0

上一篇:binary 命令

下一篇:string命令

给主人留下些什么吧!~~