在tcl 中列表是一个集合:列表里可以是数字,字符,字符串,或者是其他的列表。
创建列表的几种方法:
1、手动创建列表型变量

set lst {{item 1} {item 2} {item 3}}

2、使用split命令生成列表

set lst [split "item 1.item 2.item 3" "."]

3、使用list命令

set lst [list "item 1" "item 2" "item 3"]

调用list里的值:lindex
set lst [split “item 1.item 2.item 3” “.”]
提取lst中的第二个值:
lindex $lst 1

简要说明:

split string ?splitChars?
将字符串拆分为项目列表,无论代码中出现splitChars的位置如何。 SplitChars默认为空白。请注意,如果有两个或多个splitChars,则每个单独使用一个来拆分字符串。换句话说:split“ 1234567”“ 36”将返回以下列表:{12 45 7}。

lindex index
从列表中返回第index个项目。如果列表是其他列表的列表,则可以使用多个索引-lindex listOfLists index1 index2 …

llength
返回列表中的元素数。

可以使用foreach命令来迭代列表中的项目:

 foreach {a b} $listofpairs { ... }

您甚至可以一次从多个列表中获取一个变量!例如:

    foreach a $listOfA b $listOfB {...}
# or:foreach {a b} $listOfAB {...}

此外,列表可以嵌套:

 set nestedList {{1 2 3}{A B C D}{xyz 33 1A}}puts "The last element from the second sublist is:"puts "    [lindex $nestedList 1 end]"

运行结果:

The last element from the second sublist is: D

Examples

set x "a b c d e f g h"
puts "Item at index 2 of the list {$x} is :  [lindex $x 2]\n"set y [split 7/4/1776 "/"]
puts "we celebrate on the [lindex $y 1 ]'th day of the \
[lindex $y 0]'th month \n"set z [list puts "arg 2 is $y"]
puts "A command resembles: $z\n"set i 0
foreach j $x {puts "$j is item number $i in list x"incr i
}
set i 0
foreach {a b} $x {puts "Pair $i in list x: $a ,$b"incr i
}

Resulting output

Item at index 2 of the list {a b c d e f g h} is: cWe celebrate on the 4'th day of the 7'th monthA command resembles: puts {arg 2 is 7 4 1776}a is item number 0 in list x
b is item number 1 in list x
c is item number 2 in list x
d is item number 3 in list x
e is item number 4 in list x
f is item number 5 in list x
g is item number 6 in list x
h is item number 7 in list x
Pair 0 in list x: a, b
Pair 1 in list x: c, d
Pair 2 in list x: e, f
Pair 3 in list x: g, h

Tcl Tutorial 笔记10 · list相关推荐

  1. Tcl Tutorial 笔记1 · 输出

    目录 Simple Text Output tcl的注释 输出 puts 参考 Simple Text Output tcl的注释 命令行后进行注释使用 ;# puts "Hello, Wo ...

  2. Tcl Tutorial 笔记9 · proc 参数传递与return

    目录 proc的参数 example1 example2 proc的参数 可以proc传递固定数量的参数,也可以传递数量的可变的参数传递给proc,传递给proc的参数,可以设值默认值,如需要将传递的 ...

  3. Tcl Tutorial 笔记8 · proc

    目录 proc Example Advanced usage proc 在tcl中,proc与在其他的编程语言中的函数是没有差别的. proc 命令创建一个新命令.proc命令的语法是: proc n ...

  4. Tcl Tutorial 笔记7 ·for incr

    目录 for 与 incr for 语法 Example incr Example for 与 incr for 语法 for start test next body 在这里 start 是一个对变 ...

  5. Tcl Tutorial 笔记3 ·math

    目录 expr Operators 数字上的操作 数学函数 Type conversions 数组 example Example 1 Example 2 未完待续... expr 用于进行数学计算的 ...

  6. Tcl Tutorial 笔记 ·ubuntu命令行运行tcl 命令

    ubuntu 安装tcl : sudo apt-get install tcl 在ubuntu命令行运行tcl 命令: usually via running "tclsh" or ...

  7. Tcl Tutorial 笔记6 ·while

    目录 while 语法 while 语法 while test {body} 在这里test 作为一个表达式,if test 条件成立将执行body中的内容,如果不成立将跳出该循环. 另外注意 tes ...

  8. Tcl Tutorial 笔记2 · set ““ {} [] \

    目录 变量赋值 set Example 替换 Substitutions "" Backslashes \ Example2 替换 Substitutions {} Example ...

  9. Tcl Tutorial 笔记5 ·switch

    目录 switch 语法规则 example 参考资料 switch 语法规则 switch ?options? string {pattern1 {body1}?pattern2 {body2}?. ...

最新文章

  1. myeclipse搭建php,MyEclipse常用配置图文教程
  2. 学习进度条--第七周
  3. VC中的TRACE宏:
  4. Kubernetes — Calico CNI
  5. Java中的隐藏实现细节
  6. pytorch 训练过程acc_pytorch入门练手:一个简单的CNN模型
  7. 总结了一些指针易出错的常见问题(二)
  8. 微信整人假红包图片_警惕:千万别点!这些红包是假的
  9. linux自学笔记--vim和文本三剑客基础
  10. 你的 App 在 iOS 13 上被卡死了吗?
  11. 这个星期压力貌似比较大啊!
  12. 通用窗口类 Inventory Pro 2.1.2 Demo1(上)
  13. 联想Win10 更新系统后触控板无反应,无效
  14. React lazyLoad懒加载
  15. python3.6library 学习 1.introduction,2.built-infunction
  16. Spark2.1.0之初体验
  17. python的背景色怎么改_如何更改树的背景色
  18. springmvc测试类中如何引入controller与service,request,respon
  19. Windows安装和使用binwalk
  20. 微信小程序中background-attachment:fixed兼容问题

热门文章

  1. Javascript第二章中for循环第四课
  2. mybatis直接执行sql_拼多多二面:Mybatis是如何执行一条SQL命令的?
  3. python数据类型、if判断语句
  4. 使用grep命令快速定位代码位置
  5. git源代码控制客户端基本配置
  6. Server.MapPath方法的应用方法(转)
  7. “error MSB8011” 解决方法
  8. Linux之判断socket是否断开
  9. C++ 调节PCM音量
  10. 树莓派---MAC下安装AndroidThings