脚本语言TCL教程:2

1.1 for {puts "Start"; set i 0} {$i < 2} {incr i; puts "I after incr: $i"; } { ;#:过程PROC

1. 格式:proc name args body

2. 调用方法中参数可以用花括号或者双引号包含,也可以不包含

3. puts等命令中需要置换的话,需要使用方括号

例子:012_proc.tcl

proc sum {arg1 arg2} {

set x [expr $arg1+$arg2];

return $x ;#过程返回值

}

puts " The sum of 2 + 3 is: [sum 2 3]\n\n" ;#调用过程

1.2 #puts " The sum of 2 + 3 is: [sum {2 3}]\n\n" ;#出错,提示找不到:过程PROC的参数定义

1. 过程的参数赋缺省值:proc name {arg1 {arg2 value}}

2. 过程的不确定个数的参数定义:proc name {arg1 args}

例子:013_proc.tcl

proc example {first {second ""} args} { ;#参数定义:赋缺省值和不确定个数参数定义

if {$second == ""} {

puts "There is only one argument and it is: $first";

return 1;

} else {

if {$args == ""} {

puts "There are two arguments - $first and $second";

return 2;

} else {

puts "There are many arguments - $first and $second and $args";

return "many";

}

}

}

set count1 [example ONE]

set count2 [example ONE TWO]

set count3 [example ONE TWO THREE ]

set count4 [example ONE TWO THREE FOUR]

puts "The example was called with $count1, $count2, $count3, and $count4 Arguments"

1.3 :变量的作用域

x

1. 全局变量定义:global var1

2. 局部变量:upvar x y 等同于upvar 1 x y,作用有两个:一是将上一层的x的值赋给y;二是将上一层的x的地址赋给y,于是修改y等于修改x1代表作用范围,也可为23等,不能为0

例子:014_varscope.tcl

proc SetPositive {variable value } { ;#此处variable只是一个参数名,可以修改为其他的来代替变量

upvar $variable myvar ;#此处也可写为upvar 1 $variable myvar

if {$value < 0} { set myvar [expr -$value];} else {set myvar $value;}

return $myvar;

}

SetPositive x 5;

SetPositive y -5;

puts "X : $x Y: $y\n"

proc two {y} {

upvar 1 $y z ;#此处绑定了two中的zone中的y

upvar 2 x a ;# 此处绑定了主程序中的xtwo中的a

puts "two: Z: $z A: $a"

set z 1; ;# Set z, the passed variable to 1;

set a 2; ;# Set x, two layers up to 2;

}

;# A first level proc - This will be called by the global space code.

proc one {y} {

upvar $y z ;# This ties the calling value to variable z

puts "one: Z: $z" ;# Output that value, to check it is 5

two z; ;# call proc two, which will change the value

}

one y; ;# Call one, and output X and Y after the call.

puts "\nX: $x Y: $y"

1.4 LIST结构

1. list结构下标是从零开始的,引用方式是lindex list 位置-1

2. 字符串可以使用:split 字符串分隔符拆分得到一个list,缺省分隔符是空格

3. list可以直接定义:set z [list a b]

4. foreach x $list :用以列出list中的所有项

5. llength $list :用以列出list中的项数

例子:015_list.tcl

set x "a b c"

1.5 puts "Item 2 of the list {$x} is: [lindex $x 2]\n" ;#引用listLIST项的增删改

1. []中执行的命令不会改变其中变量的值,在外面单独执行会改变其值;

2. list函数列表:

序号

函数

解释

1

concat ?arg1 arg2 ..argn

合并list

2

lappend listname ?arg1 arg2 ..argn

list后增加项

3

linsert listname index arg1 ?arg2 ..argn

list中插入项

4

lreplace listname first last ?arg1 arg2 ..argn

替代list中的项

例子:016_list.tcl

set b [list a b {c d e} {f {g h}}] ;#4项:a; b; {c d e};{f {g h}}

puts "Treated as a list: $b\n"

set b [split "a b {c d e} {f {g h}}"]

puts "Transformed by split: $b\n" ;#输出为:a b \{c d e\} \{f \{g h\}\}

set a [concat a b {c d e} {f {g h}}]

1.6 puts "Concated: $a\n" ;#concat去掉了:更多LIST相关

1. list相关的函数列表:

序号

函数

解释

1

lsearch list pattern

按照某种模式查找list中的项,返回满足条件的第一项的出现位置

2

lsort list

list排序

3

lrange list first last

list中取出一个范围的项

lsort -mode list
排列列表。
-mode : -ascii
-dictionary
acsii类似,只是不区分大小写
-integer 转化为整数再比较
-real 转化为浮点数再比较
-command command 执行command来做比较

2. 通配符列表

序号

通配符

解释

1

*

代表任意字符

2

?

代表一个字符

3

\X

转义符

4

[...]

代表一个集合

例子:017_list.tcl

set list1 [list a b c]

set bpos [lsearch $list1 b]

puts "b position : $bpos" ;#返回位置值1

set list [list {Washington 1789} {Adams 1797} {Jefferson 1801} \

{Madison 1809} {Monroe 1817} {Adams 1825} ]

set x [lsearch $list Washington*] ;#返回0

set y [lsearch $list Madison*] ;#返回3

1.7 set y [lsearch $list M*] ;#返回满足条件的:字符串函数

1.字符串函数列表

序列

函数

解释

1

string length

返回字符串的长度

2

string index

返回字符串相应位置的字符

3

string range

返回字符串中一个范围内的字符子串

例子:018_string.tcl

set string "this is my test string"

puts "There are [string length $string] characters in \"$string\""

puts "[string index $string 1] is the second character in \"$string\"" ;#返回h

puts "\"[string range $string 5 10]\" are characters between the 5'th and 10'th" ;#返回”is my ”

1.8 :更多字符串函数

1.字符串函数列表

序号

函数

解释

1

string compare string1 string2

字符串比较

返回:

-1 string1string2

0 string1string2相等

1 string1string2

2

string first string1 string2

返回string1string2中第一次出现的位置;如果string2不在string1中,返回-1

3

string last string1 string2

返回string1string2中最后一次出现的位置;如果string2不在string1中,返回-1

4

string wordstart string1 index

返回string1index处的单词的开始位置

5

string wordend string1 index

返回string1index处的单词的结束位置

6

string match pattern string1

返回string1中是否满足匹配模式pattern

匹配模式的通配符:

* :任意字符

? :单个字符

\X :转义符

[...] :字符区间,例如:[a-z]

例子:019_stringcmp.tcl

set fullpath "/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17"

set relativepath "CVS/Entries"

set directorypath "/usr/bin/"

set paths [list $fullpath $relativepath $directorypath]

foreach path $paths {

set first [string first "/" $path];

set last [string last "/" $path];

;#根据开头是否是 \ 来判断是相对路径还是绝对路径

if {$first != 0} {

puts "$path is a relative path"

} else {

puts "$path is an absolute path"

}

;# If "/" is not the last character in $path, report the last word.

;# else, remove the last "/", and find the next to last "/", and

;# report the last word.

incr last;

if {$last != [string length $path]} {

set name [string range $path $last end];

puts "The file referenced in $path is $name"

} else {

incr last -2;

set tmp [string range $path 0 $last];

set last [string last "/" $tmp];

incr last;

set name [string range $tmp $last end]

puts "The final directory in $path is $name"

}

;# 如果是包含CVS,判断名字开头的大小写

if {[string match "*CVS*" $path]}{ ;#注意和lsearch格式的区分,lsearch list pattern,匹配模式是在后面

puts "$path is part of the source code control tree"

}

;#判断一个名字开头是大写还是小写字母

set comparison [string compare $name "a"]

if {$comparison >= 0} {

puts "$name starts with a lowercase letter\n"

} else {

puts "$name starts with an uppercase letter\n"

}

}

;#说明 string wordstart string wordend

set word "1 12 123"

;# 1的开始和结束位置,返回:01

puts "wordstart : [string wordstart $word 0]"

puts "wordend : [string wordend $word 0]"

;# 位置1上的空格的开始和结束位置,返回:12

puts "wordstart : [string wordstart $word 1]"

puts "wordend : [string wordend $word 1]"

;# 位置2上所在单词12的开始和结束位置,返回:24

puts "wordstart : [string wordstart $word 2]"

puts "wordend : [string wordend $word 2]"

;# 位置5上所在单词123的开始和结束位置,返回:58

puts "wordstart : [string wordstart $word 5]"

puts "wordend : [string wordend $word 5]"

;# 位置6上所在单词123的开始和结束位置,返回:58

puts "wordstart : [string wordstart $word 6]"

puts "wordend : [string wordend $word 6]"

1.9 :修改字符串函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值