实战Tcl和TK程序设计 §5 Tcl列表
时间:2007-07-02 来源:oychw
§5 Tcl列表
本章涉及的命令有: list, lindex, llength, lrange, lappend, linsert, lreplace, lsearch, lset, lsort, concat, join, and split.
列表与命令拒用相同的结构,最好是吧列表当作一种操作而不是语法.foreach在列表中广泛使用.eval也很相关.
大列表的性能一般都比较差,建议使用数组.列表不适合处理非结构化的数据.
Tcl列表
一系列值,用空格分隔,是具有特殊解释的字符串.
Table 5-1. List-related commands |
|
list arg1 arg2 ... |
Creates a list out of all its arguments. |
lindex list ?i ...? |
Returns the ith element from list. Specifying multiple index elements allows you to descend into nested lists easily. |
llength list |
Returns the number of elements in list. |
lrange list i j |
Returns the ith through jth elements from list. |
lappend listVar arg ... |
Appends elements to the value of listVar. |
linsert list index arg arg ... |
Inserts elements into list before the element at position index. Returns a new list. |
lreplace list i j arg arg ... |
Replaces elements i through j of list with the args. Returns a new list. |
lsearch ?options? list value |
Returns the index of the element in list that matches the value according to the options. Glob matching is the default. Returns -1 if not found. |
lset listVar ?i ...? newValue |
Set the ith element in variable listVar to newValue. (Tcl 8.4) |
lsort ?switches? list |
Sorts elements of the list according to the switches: -ascii, -dictionary, -integer, -real, -increasing, -decreasing, -index ix, -unique, -command command. Returns a new list. |
concat list list ... |
Joins multiple lists together into one list. |
join list joinString |
Merges the elements of a list together by separating them with joinString. |
split string splitChars |
Splits a string up into list elements, using the characters in splitChars as boundaries between list elements. |
构建列表:
实例1:用list命令创建列表:
% set x {1 2}
1 2
% set y \$foo
$foo
% set l1 [list $x "a b" $y]
{1 2} {a b} {$foo}
% set l2 [list $l1 $x]
{{1 2} {a b} {$foo}} {1 2}
%
实例2:用lappend 命令添加元素到列表:
% lappend new 1 2
1 2
% lappend new 3 "4 5"
1 2 3 {4 5}
%
实例3:用lset 设置元素的值:
可以设置list 的元素,也可以把list的一个元素再当作一个list。
% lset new "a b c"
a b c
% lset new 1 "d e"
a {d e} c
% lset new 1 0 "g h"
a {{g h} e} c
%
实例4:用contact 连接list
% set x {4 5 6}
4 5 6
% set y {2 3}
2 3
% set z 1
1
% concat $z $y $x
1 2 3 4 5 6
%
实例5:用contact 和list比较
% set x {1 2}
1 2
% set y "$x 3"
1 2 3
% set y [concat $x 3]
1 2 3
% set s { 2 }
2
% set y "1 $s 3"
1 2 3
% set y [concat 1 $s 3]
1 2 3
% set z [list $x $s 3]
{1 2} { 2 } 3
可见contact会消除一层列表结构.
获取列表元素
% llength { a b {c d} "e f g" h}
5
% set x {1 2 3}
1 2 3
% lindex $x 1
2
列表的索引从零开始计数
% set x {1 2 3 4 5 }
1 2 3 4 5
% lindex $x end-1
4
% lindex $x end
5
% lrange $x 2 end
3 4 5
%
修改列表
% set x {1 2 3 4 5 }
1 2 3 4 5
% linsert $x 3 30
1 2 3 30 4 5
% set x
1 2 3 4 5
% linsert $x -30 30
30 1 2 3 4 5
% linsert $x 30 30
1 2 3 4 5 30
%
实例6:用lreplace修改list
% set x [list a {b c} e d]
a {b c} e d
% lreplace $x 1 2 B C
a B C d
% set x
a {b c} e d
% lreplace $x 0 0
{b c} e d
%
可见原list并没有修改.
修改列表
% lsearch {here is a list} l*
3
实例7:根据值来删除列表元素
#
# Example 5-7
# Deleting a list element by value.
#
proc ldelete { list value } {
set ix [lsearch -exact $list $value]
if {$ix >= 0} {
return [lreplace $list $ix $ix]
} else {
return $list
}
}
对列表进行排序
排序不修改原值,排序的类型有:
-ascii, -dictionary, -integer, or –real
升序和降序-increasing or -decreasing
% lsort -ascii {a Z n2 n100}
Z a n100 n2
%
% lsort -dictionary {a Z n2 n100}
a n2 n100 Z
%
%
实例7: 根据last name 排序:
% proc NameCompare {a b} {
set alast [lindex $a end]
set blast [lindex $b end]
set res [string compare $alast $blast]
if {$res != 0} {
return $res
} else {
return [string compare $a $b]
}
}
% set list {{Brent B. Welch} {John Ousterhout} {Miles Davis}}
{Brent B. Welch} {John Ousterhout} {Miles Davis}
% lsort -command NameCompare $list
{Miles Davis} {John Ousterhout} {Brent B. Welch}
Split 命令
% set line {welch:*:28405:100:Brent Welch:/usr/welch:/bin/csh}
welch:*:28405:100:Brent Welch:/usr/welch:/bin/csh
% split $line :
welch * 28405 100 {Brent Welch} /usr/welch /bin/csh
join命令
% join {1 {2 3} {4 5 6}} :
1:2 3:4 5 6
%