Tcl 使用列表处理各种集合,列表允许把任意数量的值集合在一起,把集合作为一个实体进行传递,从集合中取得各成员的值。列表是元素的有序集合,哥哥元素可以有任意的字符串值。
一、基本列表结构与lindex和llength命令
最简单的列表就是包含任意各空格、制表符、换行符分隔开的任意多个元素的字符串。
lindex命令从一个列表中取得元素
% lindex {A B C D} 1
B
索引0对应第一个元素,索引end对应列表中的最后一个元素。使用end±整数或整数±整数形式的索引,参数中不能加空白符,即使这个参数被括起来也不能。如索引指向的位置超出了列表,lindex会返回空字符串。
llength 命令返回列表中元素的个数
llength
% llength {a b c d}
4
% llength a
1
% llength {}
0
在Tcl中输入文字列表时,通常使用大括号括起来。
set x {John Anne Mary Jim}
列表命令对列表元素中的大括号和反斜线的处理,与Tcl命令解析器对命令中的单词处理是一样的。
% set test1 {a b\ c d}
a b\ c d
% llength $test1
3
% lindex $test1 1
b c
% set test2 {a b\nc d}
a b\nc d
% llength $test2
3
% lindex $test2 1
b
c
% set test3 {a \} b \{ c}
a \} b \{ c
% llength $test3
5
% lindex $test3 1
}
大括号常用于列表中嵌套列表, 列表可嵌套的层数没有限制。
% lindex {a b {c d e} f} 2
c d e
二、创建列表: list、concat和lrepeat
Tcl 提供3个把字符串联合为列表的命令,每个命令可接受任意多个参数,返回一个列表。
2.1 list
list命令总是产生一个适当的列表结构。
% list {a b c} {d e} f {g h i}
{a b c} {d e} f {g h i}
2.2 concat
命令concat接受任意多个列表参数,把参数列表中的元素串接为一个打的列表。如存在某个元素是嵌套的列表,这个元素会保持为嵌套的列表。
% concat {a b c} {d e} f {g h i}
a b c d e f g h i
% concat {a b} {c {d e f}}
a b c {d e f}
2.3 lrepeat
lrepeat 命令重复一个元素集合来创建列表,集合各个元素作为单独参数给出,第一个参数指定的重复次数。
% lrepeat 3 a
a a a
% lrepeat 4 a b c
a b c a b c a b c a b c
% lrepeat 3 {a b} c
{a b} c {a b} c {a b} c
三、修改列表: lrange、linsert、lreplace、lset和lappend
3.1 lrange
lrange命令返回列表中某范围内的元素。
% set x {a b {c d} e}
a b {c d} e
% lrange $x 1 3
b {c d} e
% lrange $x 0 1
a b
3.2 linsert
linsert 命令将一个或多个元素插入已经存在的列表,从而形成新的列表。
参数: 三个或更多参数
参数一:列表
参数二:列表中第一个元素对应的索引值
参数三及其它参数:将要插入的新元素
% set x {a b {c d} e}
a b {c d} e
% linsert $x 2 X Y Z
a b X Y Z {c d} e
3.3 lreplace
lreplace 命令从列表中删除元素,且可选地在它们的位置添加新的元素。
参数:三个或更多参数
参数一: 列表
参数二、三:要删除的那部分元素的开头元素和结尾元素索引值
更多参数
# 三个参数
% lreplace {a b {c d} e} 3 3
a b {c d}
# 含有多个参数
% lreplace {a b {c d} e} 1 2 {W X} Y Z
a {W X} Y Z e
lreplace 并不直接修改变量的值,需要进行一次命令替换来修改原变量。lset命令是一个快速简练地修改元素值的方法。它获取变量名,列表中已经存在的一个元素对应的索引个,或一系列索引从而指向某个子列表的元素,以及赋给这个元素的新值。lset命令不能创建新列表,只能修改已经存在的列表。
% set person {{Jane Doe} 30 female}
{Jane Doe} 30 female
% set person [lreplace $person 1 1 31]
{Jane Doe} 31 female
% lset person 1 32
{Jane Doe} 32 female
3.4 lappend
lappend 命令可以把新元素存放在一个变量中的列表里。它获取一个存放列表的变量名作为参数,以及任意多个其它参数。
% set x {a b {c d} e}
a b {c d} e
% lappend x XX {YY ZZ}
a b {c d} e XX {YY ZZ}
四、从列表中获取元素lassign
lassign命令,将列表中的值分发到一个或多个变量中。
% puts "<$x>, <$y>, <$z>"
<a>, <b>, <c>
% lassig {d e} x y z
% puts "<$x>, <$y>, <$z>"
<d>, <e>, <>
% lassign {f g h i} x y
h i
% puts "<$x>, <$y>"
<f>, <g>
五、搜索列表:lsearch
lsearch命令在列表中查找指定的元素。
% set x {John Anne Mary Jim}
John Anne Mary Jim
% lsearch $x Mary
2
% lsearch $x Phil
-1
lresarch 返回列表中第一个与指定模式匹配的元素索引,如没有匹配的元素则返回-1。
可以通过在列表参数钱指定下列标志来设定模式匹配的方式: -exact、-glob和-regexp。还可以用-not选项对匹配结果取反。
glob: 指定匹配按照string match命令的规则进行
regexp: 指定匹配按照正则表达式规则进行
exact: 进行严格匹配
% lsearch -glob $x A*
1
默认情况下,lsearch只查找最先出现的匹配的元素,可以使用-all命令,要求将所有匹配的元素组成一个列表返回。-inline选项指定返回元素,而非元素的索引。
% set states {California Hawaii Iowa Maine Vermont}
California Hawaii Iowa Maine Vermont
% lsearch -all $states *a
0 2
% lsearch -all -inline $states *a
California Iowa
六、排序列表:lsort
lsort用于列表排序,可以设置很多选项来控制排序。
-decreasing: “最大”的元素放在最前面
-integer和-real: 指定列表中的元素视为整数或实数,然后按其值大小进行排序
-dictionary: 指定不区分大小写的排序,且元素中嵌入的数字作为非负整数处理
-unique: 返回的结果中,原列表里重复出现的元素只会出现一次
% lsort -decreasing {John Anne Mary Jim}
Mary John Jim Anne
% lsort {10 1 2}
1 10 2
% lsort -integer {10 1 2}
1 2 10
% lsort {Peach banana Apple pear}
Apple Peach banana pear
% lsort -dictionary {Peach banana Apple pear}
Apple banana Peach pear
% lsort -dictionary {n11.gif n1.gif n10.gif n9.gif}
n1.gif n9.gif n10.gif n11.gif
% lsort -unique {c a b q a z q}
a b c q z
对于嵌套的列表结构,-index选项允许指定子列表中的元素的索引,根据指定的元素对子列表进行排序。
% lsort -integer -index 1 {{First 24} {Second 18} {Third 30}}
{Second 18} {First 24} {Third 30}
七、字符串和列表转化:split与join
split命令将字符串分成几个部分,然后可以对各个部分独立地进行处理。
% set x "Anita Sanchez, 35, VP Marketing"
Anita Sanchez, 35, VP Marketing
% set y 39,72,,-17,
39,72,,-17,
% split $x ,
{Anita Sanchez} { 35} { VP Marketing}
% split $y ,
39 72 {} -17 {}
可以设定由多个字符组成分隔符,如分隔字符是空字符串,则会把字符串的每一个字符都分开,作为新列表的独立元素。
% split xbaybz ab
x {} y z
% split {a b c} {}
a { } b { } c
join命令大体上是split命令的逆操作,把列表元素串接成一个字符串,元素间用指定分隔符隔开。
% join {{} usr include sys types.h} /
/usr/include/sys/types.h
% set x {24 112 5}
24 112 5
% expr [join $x +]
141
202

被折叠的 条评论
为什么被折叠?



