在上一章中我们提到了列表,在本章中做一个详细一些的介绍。
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 |