1.split string delimeter
将字符串分成几个部分,然后可以对各个部分独立进行处理,返回值为列表,第一个参数为待分割字符,第二个为一个或多个分割符
% set x "anita san,35,vp marketing"
anita san,35,vp marketing
% split $x ,
{anita san} 35 {vp marketing}
% set y 38,72,,-17
38,72,,-17
% split $y ,
38 72 {} -17
%
多个字符组成的分隔符
% split xbaybz ab
x {} y z
如果指定分隔符是空字符串,会把字符串的每一个字符都分开
% split {a b c} {}
a { } b { } c
% split {a b c} { }#空格
a b c
%
2.join
为split的逆操作,它把列表元素串接成一个字符串,元素之间用指定的分隔符隔开
% join { {} usr include sys types.h} /
/usr/include/sys/types.h
% join {usr include sys types.h} /
usr/include/sys/types.h
%
%set x 50 50 41
3.dict get
从字典取得一个元素,字典即哈希,字典由键值组成
% set ex {firstname joe surname schmoe title mr}
firstname joe surname schmoe title mr
% dict get $ex surname
schmoe
在TCL脚本中使用字典,字典常常被大括号括起来,对于比较复杂的字典,可以使用换行吧各个关键词-关联值对分割开。
set prefers {
joe {the easy life}
jermy {fast car}
{uncle sam} {motherhood and apple pie}
hell world
}
joe {the easy life}
jermy {fast car}
{uncle sam} {motherhood and apple pie}
hell world
% dict get $prefers hell
world
% dict get $prefers joe
the easy life
% set employees {
0001 {
firstname joe
surname schmoe
title mr
}
0002 {
firstname ann
initial e
surname huan
title miss
}
}
0001 {
firstname joe
surname schmoe
title mr
}
0002 {
firstname ann
initial e
surname huan
title miss
}
% dict get [dict get $employees 0002] surname
huan
dict get 可以接受多个参数
% dict get $employees 0001 firstname
joe
% set g(0,1) 01
01
% set g(0,2) 02
02
% array get g 0,*
0,1 01 0,2 02
注意array get 不带 $
4.创建字典dict create
% dict create a b c {d e} {f g} h a "b repeat"
a {b repeat} c {d e} {f g} h
当一个关键字多次出现时,采用最后与它关联的值
5.dict replace
% set ex [dict create firstname ann initial e surname huan]
firstname ann initial e surname huan
% dict replace $ex initial y
firstname ann initial y surname huan
%
6.dict remove,注意试图删除一个不存在的键并不会导致错误
% dict remove $ex initial
firstname ann surname huan
7.dict merge 融合两个或更多字典,创建一个新字典,当两个或多个字典由同样的关键字时,删除的字典会把这个关键字映射到命中做活出现该关键字的字典中的关联值
% set c1 {foreground white background black}
foreground white background black
% set c2 {highligh red foreground green}
highligh red foreground green
% dict merge $c1 $c2
foreground green background black highligh red
%
8.dict set ,dict unset
% set ex [dict create firstname anne initial e surname huan title miss]
firstname anne initial e surname huan title miss
% dict set ex title mrs
firstname anne initial e surname huan title mrs
% dict get $ex title
mrs
% dict set ex surname boddie
firstname anne initial e surname boddie title mrs
% dict get $ex surname
boddie
% dict unset ex initial
firstname anne surname boddie title mrs
% dict get $ex initial
key "initial" not known in dictionary
%
9.dict size,keys,values,exists
% set ex {}
% dict set ex a alpha
a alpha
% dict set ex b bravo
a alpha b bravo
% dict set ex c charlie
a alpha b bravo c charlie
% dict size $ex
3
% dict exists $ex a
1
% dict exists $ex initial
0
% dict keys $ex
a b c
% dict keys $ex {abc}
% dict keys $ex {*name}
% dict values $ex
alpha bravo charlie
% dict values $ex *n*
% dict values $ex *r*
bravo charlie
%
10.dict for
获取一对变量的列表作为一个参数,该命令的返回值为空字符串,可以使用break,continue停止循环或调到字典中的下一个关键字-关联值对
% dict for {key value } $ex {
puts [format "%s: %s" $key $value]
}
a: alpha
b: bravo
c: charlie
%
% proc sortdict {dict} {
set sorted {}
foreach key [lsort [dict keys $dict]] {
dict set sorted $key {}
}
return [dict merge $sorted $dict]
}
% set ex [dict create ba 3 d 66 a 77]
ba 3 d 66 a 77
% sortdict $ex
a 77 ba 3 d 66
%