TCL脚本语言学习

TCL是一种脚本语言,它几乎可以在全部平台上运行,可移植行很强。
TCL语言设计的目的是提供程序与其他程序之间进行交互的功能,也是作为一个可嵌入的翻译互相作用的能力。
开发简单,上手快。

TCL输入输出

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/tclsh
#默认输出到标准输出流
puts hello
puts stdout hello
#输出到标准错误流
puts stderr error
#输入必须指定输入流
gets stdin varible
puts $varible

TCL文件读写操作

1
2
3
4
5
6
7
8
9
#打开data文件,打开模式有r,w,a,r+,w+,a+
set fp [open data w]
puts $fp "hello world"
close $fp
set fp [open data r]
gets $fp test
puts $test
close $fp

TCL数据类型

TCL变量不需要声明,可以直接使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# TCL对象,对于整数,浮点数,bool,字符串,都是一个对象
#可直接给其赋值
set varible 1
set s "hello world"
# 列表,列表初始化时可以使用双引号或者大括号进行初始化
set list {hello world hah hah}
set list "hello world hah hah"
#访问列表元素,使用lindex
puts [lindex $list 0] #hello
#关联数组,类似map,key可以是数字,也可以是字符串
set map(a) 10
puts $map(a)

TCL条件控制语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# if语句
if {expression1} {
dosomting
} elseif {expression2} {
dosometing
} else {
dosomething
}
#for 语句
for {set a 10} {$a < 20} {incr a} {
dosometing
}
#switch语句
switch switchingString {
matchString1 {
body1
}
matchString2 {
body2
}
matchStringn {
bodyn
}
}

运算符的优先级

分类 操作符 关联
Unary + - Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR Left to right
Logical AND && Left to right
Logical OR || Left to right
Ternary ?: Right to left