`
universsky
  • 浏览: 92874 次
文章分类
社区版块
存档分类
最新评论

脚本语言TCL教程: 1

 
阅读更多

1.1 :简单文本输出

1 注释符号是#或者 ;# ,在命令后注释用 ;# ,在行开头两者均可;

2 puts :输出文本,多个单词如被空格或TAB分隔需要使用“”或{} 括起来;

3 多个命令写在一行使用 ; 间隔。

例子:002_puts.tcl

# ok ;# 正确

;# ok ;# 正确

; # ok ;# 正确, 分号和井号之间可以有空格

puts Hello ;# 正确

puts Hello,World ;# 正确,多个单词之间不是被空格或者TAB分隔开

puts Hello World ;# 这行命令运行出错,被空格分隔

puts "Hello, World - In quotes" ;# 注释

puts {Hello, World - In Braces} # 这行命令运行出错,必须使用 ;# 作为注释符号

puts "This is line 1"; puts "this is line 2" ;# 正确,用分号分隔两个命令

puts "Hello, World; - With a semicolon inside the quotes" ;#正确,分号在双引号内,作为字符串一部分

1.2 :给变量赋值

1. set:给变量赋值,格式为 set var value

例子:003_var.tcl

;# 给变量X赋一个字符串

set X "This is a string"

;# 给变量Y赋一个数字

set Y 1.24

;# 显示XY的内容

puts $X

puts $Y

;# 打印一个分隔串

puts "..............................."

;# 打印在一行中,推荐使用双引号

set label "The value in Y is: "

puts "$label $Y"

puts $label$Y

1.3 :命令的赋值与置换一

1. TCL中命令的赋值分为置换和赋值两个步骤

2. 续行符为 \

3. 转义符同为 \

4. 特殊字符列表:

序号

字符

输出

十六进制

1

\a

响铃

\x07

2

\b

回车

\x08

3

\f

清屏

\x0c

4

\n

换行

\x0a

5

\r

回车

\x0d

6

\t

制表符

\x09

7

\v

垂直制表符(Vertical Tab)

\x0b

8

\ddd

八进制值

d=0-7

9

\xhh

十六进制值

h=0-9,A-F,a-f

例子:004_eval.tcl

;# Show how a \ affects the $

set Z "Albany"

set Z_LABEL "The Capitol of New York is: "

puts "$Z_LABEL $Z" ;#显示Albany

puts "$Z_LABEL \$Z" ;#显示$Z,被 \ 转义

;# The next line needs a backslash to escape the '$'

puts "\nBen Franklin is on the \$100.00 bill" ;# \n换行; $100前的 \ 必须有,否则会将100作为一个变量,提示出错

set a 100.00

puts "Washington is not on the $a bill" ;# This is not what you want

puts "Lincoln is not on the $$a bill" ;# 显示$100,说明是后结合的,先置换了$a,此处严格的写应该写为 \$$a

puts "Hamilton is not on the \$a bill" ;# 显示$a

puts "Ben Franklin is on the \$$a bill" ;# 显示$100,说明是后结合的,先置换了$a

puts "\n................. examples of escape strings"

puts "Tab\tTab\tTab"

puts "This string prints out \non two lines" ;# 行中 \ 没有打印出来,如果要打印出来,需要写成 \\

puts "This string comes out\

on a single line" ;# 当一行太长,不便于阅读,使用 \ 做续行符

1.4 :命令的赋值与置换二

1.最外层是 {} 则不会进行置换操作,但其中的续行符仍然有效

例子:005_escape.tcl

set Z "Albany"

set Z_LABEL "The Capitol of New York is: "

puts "\n................. examples of differences between \" and \{" ;#and前的双引号前必须有\ 进行转义,否则这个双引号回和前面的双引号结合, 导致成了 “xxx” and \{“ 的结构,会提示出错

puts "$Z_LABEL $Z" ;# 显示The Capitol of New York is: Albany

puts {$Z_LABEL $Z} ;# 显示 $Z_LABEL $Z,没有进行置换,{}中不会置换

puts "\n....... examples of differences in nesting \{ and \" "

puts "$Z_LABEL {$Z}" ;# 最外层是双引号,所以进行了置换

puts {Who said, "What this country needs is a good $Z cigar!"?} ;#最外层是花括号,所以没有进行置换

puts "\n................. examples of escape strings"

puts {There are no substitutions done within braces \n \r \x0a \f \v} ;#

puts {But, the escaped newline at the end of a\

string is still evaluated as a space} ;#续行符仍然生效

1.5 :命令的赋值与置换三

1. [ ] 可以传递其中的命令结果,注意不能被 {} 包含;

2. 双引号包含的 [ ] 中的命令可以正常执行,命令结果也可以传出;

3. {} 包含的 [ ] 中的命令不会执行,更不会有命令结果传出来。

例子:006_escape.tcl

set x "abc"

puts "A simple substitution: $x\n" ;#显示abc

set y [set x "def"] ;#先执行[]中的命令,”def”赋值给x,然后将该命令的结果赋值给y

puts "Remember that set returns the new value of the variable: X: $x Y: $y\n" ;#显示xy都是def

set z {[set x "This is a string within quotes within braces"]} ;#由于在{},所以并没有执行对x的赋值,只是将{}赋值给z

puts "Note the curly braces: $z\n"

set a "[set x {This is a string within braces within quotes}]" ;#执行了对x的赋值操作,并将值传出来赋给了a

puts "See how the set is executed: $a"

puts "\$x is: $x\n"

set b "\[set y {This is a string within braces within quotes}]"

puts "Note the \\ escapes the bracket:\n \$b is: $b"

puts "\$y is: $y"

1.6 :算数运算

1. 操作符

序号

操作符

解释

1

- + ~ !

- : 负号 + : 正号 ~ : 位操作非 ! : 逻辑非

2

* / %

* : / : % : 取模

3

+ -

+ : - :

4

<< >>

<< : 循环左移 >> : 循环右移

5

&

& : 按位与

6

^

^ : 按位异或

7

|

| : 按位或

8

&&

&& : 逻辑与

9

||

|| : 逻辑或

10

x?y:z

if-then-else

2. 数学函数

序号

函数

序号

函数

1

acos

11

log10

2

cos

12

tan

3

hypot

13

atan2

4

sinh

14

floor

5

asin

15

pow

6

cosh

16

tanh

7

log

17

ceil

8

sqrt

18

fmod

9

atan

19

sin

10

exp

例子:007_math.tcl

set X 100;

set Y 256 ;# 行末是否有分号都可以

set Z [expr "$Y + $X"] ;# 变量是否被双引号包含都可以,不过建议使用双引号

set Z [expr $Y + $X]

set Z_LABEL "$Y plus $X is "

puts "$Z_LABEL $Z"

puts "The square root of $Y is [expr sqrt($Y)]\n"

puts "Because of the precedence rules \"5 + -3 * 4\" is: [expr -3 * 4 + 5]"

puts "Because of the parentheses \"(5 + -3) * 4\" is: [expr (5 + -3) * 4]"

puts "\n................. more examples of differences between \" and \{"

puts {$Z_LABEL [expr $Y + $X]} ;#外层是花括号不会进行置换

puts "$Z_LABEL {[expr $Y + $X]}" ;# 外层是双引号会进行置换

puts "The command to add two numbers is: \[expr \$a + \$b]"

1.7 :文本比较-SWITCH应用

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分支,而分支中的命令仍然被执行了

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"}

}

1.8 :数值比较-IF应用

1. 条件式结果

FALSE

TRUE

数值

0

非零

yes / no

no

yes

true / false

false

true

2.置换变量的方法,set y x ; puts $$y ,因为是后结合并且是一次置换,所以打出来的是 $x ,不是$x的值;但是在if的条件式中进行了二次置换, $$y 被置换成了 $x 的值

3.注意:新行中需要写为 } else { ,不能将 } 写到前一行的末尾,也不能省略 } 后面的那个空格,后面的 { 也需要写在当行,并且前面需要一个空格。

例子:009_if.tcl

set x 1;

if {$x == 2} {puts "$x is 2"} else {puts "$x is not 2"} ;#判断是否相等使用 ==

if {$x != 1} { ;#判断是否不等使用 !=

puts "$x is != 1"

} else {

puts "$x is 1"

}

if $x==1 {puts "GOT 1"}

set y x;

if "$$y != 1" { ;#if条件式中$$y进行了二次置换

puts "$$y is != 1" ;#puts命令中,只进行了一次置换

} else {

puts "$$y is 1"

}

1.9 WHILE 循环

x

1while后面的条件表达式是放在花括号中的;放在双引号中会只执行一次置换

例子:010_while.tcl

set x 1;

while {$x < 5} {puts "x is $x"; set x [expr $x + 1]}

puts "exited first loop with X equal to $x\n"

set x 0;

while "$x < 5" { ;#只执行一次置换1<5,于是该条件永远为真

set x [expr $x + 1]

if {$x >6} break; ;#如果去掉这句就成了死循环

if "$x > 3" continue; ;#这句使4 打不出来

puts "x is $x";

}

puts "exited second loop with X equal to $x\n"

1.10FOR循环和incr

1incr x set x [expr $x + 1] 达到一样的效果,向上加一

x

例子:011_for.tcl

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics