清霜一梦

Tcl语言笔记之二

0
阅读(1313)

1,表达式

1.1 操作数 TCL表达式的操作数通常是整数或实数。整数一般是十进制的, 但如果整数的第一个字符是0(zero),那么TCL将把这个整数看作八进制的,如果前两个字符是0x则这个整数被看作是十六进制的。

1.2运算符  TCL语法形式和用法跟ANSI C中很相似

1.3 函数

    TCL中支持的数学函数如下

abs( x) Absolute value of x. 
acos( x) Arc cosine of x, in the range 0 to p. 
asin( x) Arc sine of x, in the range -p/2 to p/2. 
atan( x) Arc tangent of x, in the range -p/2 to p/2. 
atan2( x, y) Arc tangent of x/ y, in the range -p/2 to p/2. 
ceil( x) Smallest integer not less than x. 
cos( x) Cosine of x ( x in radians). 
cosh( x) Hyperboliccosine of x.

double( i) Real value equal to integer i. 
exp( x) e raised to the power x. 
floor( x) Largest integer not greater than x. 
fmod( x, y) Floating-point remainder of x divided by y. 
hypot( x, y) Square root of ( x 2 + y 2 ). 
int( x) Integer value produced by truncating x. 
log( x) Natural logarithm of x. 
log10( x) Base 10 logarithm of x. 
pow( x, y) x raised to the power y. 
round( x) Integer value produced by rounding x. 
sin( x) Sine of x ( x in radians). 
sinh( x) Hyperbolic sine of x. 
sqrt( x) Square root of x. 
tan( x) Tangent of x ( x in radians). 
tanh( x) Hyperbolic tangent of x.

TCL中有很多命令都以表达式作为参数。最典型的是expr命令,另外if、while、for等循环控制命令的循环控制中也都使用表达式作为参数。

 

2,list

2.1   list命令

list这个概念在TCL中是用来表示集合的。TCL中list是由一堆元素组成的有序集合,list可以嵌套定义

        % list 1 2 {3 4}  
        1 2 {3 4}

2.2 concat 命令(这个命令不知道怎么用)

           语法:concat list ?list...?

        这个命令把多个list合成一个list,每个list变成新list的一个元素

2.3 lindex 命令

      语法:lindex list index  
      返回list的第index个(0-based)元素。例: 
      % lindex {1 2 {3 4}} 2  
       3 4

2.4 llength 命令

    语法:llength list  
    返回list的元素个数。例 
    % llength {1 2 {3 4}}  
    3

2.5 linsert 命令

      语法:linsert list index value ?value...?  
      返回一个新串,新串是把所有的value参数值插入list的第index个(0-based)元素之前得到。
      例: 
      % linsert {1 2 {3 4}} 1 7 8 {9 10}  
      1 7 8 {9 10} 2 {3 4}

2.6 lreplace 命令

      语法:lreplace list first last ?value value ...?  
      返回一个新串,新串是把list的第firs (0-based)t到第last 个(0-based)元素用所有的value
      参数替换得到的。如果没有value参数,就表示删除第first到第last个元素。例: 
      % lreplace {1 7 8 {9 10} 2 {3 4}} 3 3  
      1 7 8 2 {3 4}  
      % lreplace {1 7 8 2 {3 4}} 4 4 4 5 6  
       1 7 8 2 4 5 6

2.7 lrange 命令

        语法:lrange list first last  
        返回list的第first (0-based)到第last (0-based)元素组成的串,如果last的值是end。就是
        从第first个直到串的最后。 
        例:
        % lrange {1 7 8 2 4 5 6} 2 end  
        8 2 4 5 6

2.8 lappend 命令

      语法:lappend varname value ?value...?  
      把每个value的值作为一个元素附加到变量varname后面,并返回变量的新值,如果varname
      不存在,就生成这个变量。例:

       %set  a  9

      9 
      % lappend a 1 2 3  
       9  1 2 3  
      % set a  
      9  1 2 3

 2.9  lsearch 命令

      语法:lsearch ?-exact? ?-glob? ?-regexp? list pattern  
      返回list中第一个匹配模式pattern的元素的索引,如果找不到匹配就返回-1。-exact、-glob、
      -regexp是三种模式匹配的技术。-exact表示精确匹配;-glob的匹配方式和string match
      命令的匹配方式相同,将在后面第八节介绍string命令时介绍;-regexp表示正规表达式匹配,
      将在第八节介绍regexp命令时介绍。缺省时使用-glob匹配。例: 
      % set a { how are you }  
      how are you  
      % lsearch $a y*  
       2 
      % lsearch $a y?  
      -1

 2.10 lsrot命令

语法:lsort ?options? list  
这个命令返回把list排序后的串。options可以是如下值: 
-ascii 按ASCII字符的顺序排序比较.这是缺省情况。 
-dictionary 按字典排序,与-ascii不同的地方是:
(1)不考虑大小写
(2)如果元素中有数字的话,数字被当作整数来排序. 
因此:bigBoy排在bigbang和bigboy之间, x10y 排在x9y和x11y之间. 
-integer 把list的元素转换成整数,按整数排序. 
-real 把list的元素转换成浮点数,按浮点数排序. 
-increasing 升序(按ASCII字符比较) 
-decreasing 降序(按ASCII字符比较) 
-command command TCL自动利用command 命令把每两个元素一一比较,然后给出排序结果。

2.11 split 命令

语法:split string ?splitChars?  
把字符串string按分隔符splitChars分成一个个单词,返回由这些单词组成的串。如果
splitChars 
是一个空字符{},string被按字符分开。如果splitChars没有给出,以空格为分隔符。例: 
% split "how.are.you" .  
how are you  
% split "how are you"  
how are you  
% split "how are you" {}  
h o w { } a r e { } y o u

 

2。12 join 命令

语法:join list ?joinString?  
join命令是命令的逆。这个命令把list的所有元素合并到一个字符串中,中间以joinString分
开。缺省的joinString是空格。例: 
% join {h o w { } a r e { } y o u} {}  
how are you  
% join {how are you} .  
how.are.you