TCL基础教程——(6)列表
来源:互联网 发布:jsp页面引入java类 编辑:程序博客网 时间:2024/06/05 14:34
在上一章中我们提到了列表,在本章中做一个详细一些的介绍。
TCL列表是一个系列的值,使用空格进行分割,很容易让人想到C++和JAVA语言中的数组。在TCL中,通过list命令来将一个系列的值变为一个列表,如
set numberlist [list 1 2 3 4 5 6]
numberlist就是一个简单的列表。
[ppcorn@localhost ppcorn]$ cat numlist.tcl
#!/usr/bin/tclsh
set numberlist [list 1 2 3 4 5 6]
puts $numberlist
[ppcorn@localhost ppcorn]$ ./numlist.tcl
1 2 3 4 5 6
对于列表来说,常用的操作有以下:
命令
用法
list arg1 arg2
根据所有的值构造一个列表
lindex list i
返回list的第i个元素
llength list
返回list的元素个数
lrange i j
返回list中从第i个到第j个的元素
lappend list arg1 arg2
将元素追加到list的后面
linsert list index arg
将元素追加到list中位于index之前的位置
lreplace list i j arg arg
将list中从i到j的元素替换为args
lsearch ?mode? list value
根据mode(-exact –glob或-regexp)返回list中与value匹配的元素索引,如果没有就返回-1
lsort ?switches? list
根据开关选项对list进行排序
concat list list
将多个list连接
join list joinstring
以joinstring为分隔符,将列表中的元素合并在一起
split string split chars
使用split chars中的字符串作为列表元素的分割
很容易看出来,对单个列表进行操作的命令,都是以l(list)开头。
就列表而言,应该来说使用机会较多,下面使用程序对上述的命令进行演示。
ppcorn@localhost:~/tcl$ cat listtest.tcl
#!/usr/bin/tclsh
set numberlist [list 1 2 3 4 5 6 7 8 9 0]
# lindex to put the value of No.9, it is 0
puts [lindex $numberlist 9]
# llength to return the length of list, it is 10
puts [llength $numberlist]
# lrange to return to value from No.4 to No.6, it is 5 6 7
puts [lrange $numberlist 4 6]
# lappend to add a var to the end of list
# The value should be 1 2 3 4 5 6 7 8 9
lappend numberlist a
puts $numberlist
# linsert to insert a var before the postion and return a new list
# it will not change the value of numberlist
# The numberlist1 should be 1 2 3 4 5 b 6 7 8 9
set numberlist1 [linsert $numberlist 5 b]
puts $numberlist
puts $numberlist1
# lreplace to replace the value from i to j with arg and return a new list
# it will not change the numberlist
# The numberlist2 should be
set numberlist2 [lreplace $numberlist 1
puts $numberlist
puts $numberlist2
# lsearch
# the value should be 8
puts [lsearch $numberlist 9]
# lsort
# the value should be 0 1 2 3 4 5 6 7 8
puts [lsort $numberlist]
# concat
# The value should be 1 2 3 4 5 6 7 8 9
puts [concat $numberlist [list a b c d]]
# join
# the value should be 1:2 3 : 4 5 6
puts [join {1 {2 3} {4 5 6} } : ]
# split
# the value should be ab defg
puts [split abcdefg c]
ppcorn@localhost:~/tcl$ ./listtest.tcl
0
10
5 6 7
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 b 6 7 8 9
1 2 3 4 5 6 7 8 9
8
0 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1:2 3:4 5 6
ab defg
如果需要熟练掌握上述的列表功能的话,我想,经过一段时间的运用就可以了。
- TCL基础教程——(6)列表
- TCL基础教程——(5)TCL中的结构控制
- TCL基础教程——(1)准备
- TCL基础教程——(8)数组
- TCL基础教程——(2)输出和变量
- TCL基础教程——(3)数学计算
- TCL基础教程——(4)字符串处理
- TCL基础教程——(7)过程和作用域
- python基础教程——列表
- tcl/tk参考——列表操作join
- tcl/tk参考——列表操作concat
- tcl/tk参考——列表操作llength
- tcl/tk参考——列表操作linsert
- tcl/tk参考——列表操作lindex
- tcl/tk参考——列表操作lassign
- tcl/tk参考——列表操作list
- tcl/tk参考——列表操作lappend
- tcl/tk参考——列表操作lrange
- 载在胜利光环昂首离开!
- TCL基础教程——(4)字符串处理
- 串行通信波特率的一种自动检测方法
- 学习boost的一些我的体会
- TCL基础教程——(5)TCL中的结构控制
- TCL基础教程——(6)列表
- 串口通信基本接线方法
- 单片机CRC算法与实现
- XHTML 和 DOCTYPE 切换(MSDN)
- 用MFC实现串口编程
- asp.net技术的学习顺序问题
- Delphi 中加入的SQL如何动态的处理串中的单撇号
- ASP.NET2.0 文本编辑器FCKeditor使用
- TCL基础教程——(7)过程和作用域