---- 数组是一些元素的集合。Tcl的数组和普通计算机语言中的数组有很大的区别。
在Tcl中,不能单独声明一个数组,数组只能和数组元素一起声明。
数组中,数组元素的名字包含两部分:数组名和数组中元素的名字,Tcl中数组元素的名字(下标)可以为任何字符串。
% set day(monday) 1
% set day(tuesday) 2
上面的命令:第一个命令生成一个名为day的数组,同时在数组中生成一个名为monday的数组元素,并把值置为1.
第二个命令生成一个名为tuesday的数组元素,并把值置为2.
set a monday
set day(monday) 1
set b $day(monday) //b 的值为1,即day(monday)的值。
set c $day($a) //c 的值为1,即day(monday)的值
---- append 和incr
---- append 命令把文本加到一个变量的后面,例如:
% set txt hello
hello
% append txt "! How are you"
hello! How are you
---- incr 命令把一个变量值加上一个整数。incr 要求变量原来的值和新加的值都必须是
整数。
%set b a
a
% incr b
expected integer but got "a"
%set b 2
2
%incr b 3
5
---- TCL 中支持的数学函数如下:
abs( x) Absolute value of x.
ceil( x) Smallest integer not less than x.
floor( x) Largest integer not greater than x.
pow( x, y) x raised to the power y.
round( x) Integer value produced by rounding x.
sqrt( x) Square root of x.
---- concat命令
语法:concat list ?list...?
这个命令把多个list 合成一个list,每个list 变成新list 的一个元素。
---- lindex命令
语法:lindex list index
返回list 的第index 个(0-based)元素。例:
% lindex {1 2 {3 4}} 2
3 4
---- llength命令
语法:llength list
返回list 的元素个数。例:
% llength {1 2 {3 4}}
3