Tcl基础学习(2):数组和数据结构-list
数组
数组是一组使用索引对应元素的排列方式。
常规的数组语法为:
- 创建简单的数组 -- set ArrayName(Index) value
- 计算数组大小 -- [array size variablename]
- 检索数组索引 -- [array names variablename]
#创建简单的数组
set test(0) Hello
set test(1) "Hello world"
puts $test(0)
puts $test(1)
#打印数组的大小
puts [array size test]
#打印数组的索引
puts [array names person]
#执行结果
Hello
Hello world
2
0 1
关联数组:所有数组本质是相关联的。数组存储并没有任何具体的顺序进行检索。关联数组的索引下标既可以是数字也可以是字符串。
#打印数组的索引
set person(name) "na"
set person(age) 14
puts $persom(name)
puts $person(nage)
puts [array names person]
#执行结果
na
14
age name
数组的迭代
在索引连续的数组中,可以使用迭代的方式去访问数组的元素。
#数组迭代
set data(0) "one"
set data(1) "two"
set data(2) "three"
for { set index 0 } { $index < [array size data] } { incr index } {
puts "data($index) : $data($index)"
}
#incr 递增加1
#执行结果
data(0) : one
data(1) : two
data(2) : three
List (列表)
List是Tcl的一个基本数据结。一个List就是一个有序集合,同一个列表中可以包含不同的类型:数字、单词 、字符串、另一个列表或List的其他形式。
创建list方式
- 通过设置一个变量为由多个值组成的列表-- set lst {{item1} {item2} {item3}}
- 使用split命令 -- set lst [split "item1.item2.item3" "."]
- 使用list命令 -- set lst [list "item1" "item2" "item3"]
- 使用concat -- set lst [concat arg1 arg2 ... argn]
注:split string ?splitChars? 是把string中分割成多个值组成list,分割符是splitChars,默认是空格
concat ?arg1 arg2 ... argn? 中的args既可以是一个单个的 元素,也可以是一个list列表,如果是一个arg已经是list,它的内容会和其他arg连接在一起
一个独自的list成员可以通过lindex下标命令访问:
lindex $list index -- 返回第index个列表项(下标从0开始)
llength $list -- 返回列表元素的个数,可通过使用foreach命令进行枚举
foreach varname $list {body} -- 对每一个列表项执行body中的代码,每一次,varname会包含list的下一个变量。可以同时接受多个来自列表的元素:foreach {a b} $lst {...},也可以同时接受来自多个列表的单个变量:foreach a $lst1 b $lst2 {...}
#创建列表x
set x "a b c";
puts "Item at index 2 of the list{$x} is : [lindex $x 2]\n";
#创建列表y
set y {split "12/3456" "/4"};
puts "The length of the list{$y} is : [llength $y]\n";
#创建列表z
set z [list puts "arg 2 is $y"];
puts "A command resemble : $z\n";
#创建列表m
set m [concat a b {cd} {e f {g h}}]
puts "Treated as a list : $m\n"
#枚举foreach
set i 0;
foreach j $x{
puts "$j is item number $i in list x";
incr i;
}
#执行结果
a b c
Item at index 2 of the list{a b c} is : c
12 3 56
The length of the list{12 3 56} is : 3
a b cd e f {g h}
Treated as a list : a b cd e f {g h}
puts {arg 2 is 12 3 56}
A command resemble : puts {arg 2 is 12 3 56}
0
a is item number 0 in list x
b is item number 1 in list x
c is item number 2 in list x
关于list列表的其他命令
命令 | 功能描述 |
lappend listname ?arg1 arg2 ... argn? | 追加 args 项目到 listname 列表,每一个 arg 都被当做一个列表元素 |
linsert listname index arg1 ?arg2..argn? | 返回一个新的 list 即 listname 的第 index 个元素之前被插入了多个元素形成的列表 |
lreplace listname first last ?arg1 arg2..argn? | 返回一个新列表,即 listname 的 N 个元素被 args 替换掉后的列表,如果 first <=0 ,从0开始替换, first > end 相当于 lappend |
lset varname index newValue | 直接设置列表的元素,来替代 lreplace |
lsearch $list pattern | 搜索一个 匹配的 pattern 的 list 项,返回第一个匹配项的下标,如果没有匹配项,返回-1 |
lsort $list | 对 list 中元素进行排序,返回经过排序的新 list ,默认情况下,按照字母顺序排列。 |
lrange $list first last | 返回由 first 和 end 之间的项组成的列表。 first <=0 ,从0到end;first <= end ,first = end在末尾; first > end,返回空列表 |
#创建列表x = a b c d
set x [list a b c d]
#追加
lappend x e f g
puts "After lappend : $a\n"
#插入
set y [linsert $x 2 123]
puts "After linsert at position 2 : $b\n"
#替换
set z [lreplace $b 3 4 "AA" "BB"]
lset z 6 "CC"
puts "After lreplacing 3 positions with 2 values at position 3 : $z\n"
#搜索
lsearch $y 123
lsearch $z A
lsearch $z A*
puts "--------------\n"
#排序
set m [lsort $z]
puts "After lsort : $m\n"
#lrange
set n [lrange $m 1 5]
#执行结果
a b c d
a b c d e f g
After lappend : a b c d e f g
a b 123 c d e f g
After linsert at position 2 : a b 123 c d e f g
a b 123 AA BB e f g
a b 123 AA BB e CC g
After lreplacing 3 positions with 2 values at position 3 : a b 123 AA BB e CC g
2
-1
3
--------------
123 AA BB CC a b e g
After lsort : 123 AA BB CC a b e g
AA BB CC a b