Tcl Tutorial 笔记10 · list
在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相关推荐
- Tcl Tutorial 笔记1 · 输出
目录 Simple Text Output tcl的注释 输出 puts 参考 Simple Text Output tcl的注释 命令行后进行注释使用 ;# puts "Hello, Wo ...
- Tcl Tutorial 笔记9 · proc 参数传递与return
目录 proc的参数 example1 example2 proc的参数 可以proc传递固定数量的参数,也可以传递数量的可变的参数传递给proc,传递给proc的参数,可以设值默认值,如需要将传递的 ...
- Tcl Tutorial 笔记8 · proc
目录 proc Example Advanced usage proc 在tcl中,proc与在其他的编程语言中的函数是没有差别的. proc 命令创建一个新命令.proc命令的语法是: proc n ...
- Tcl Tutorial 笔记7 ·for incr
目录 for 与 incr for 语法 Example incr Example for 与 incr for 语法 for start test next body 在这里 start 是一个对变 ...
- Tcl Tutorial 笔记3 ·math
目录 expr Operators 数字上的操作 数学函数 Type conversions 数组 example Example 1 Example 2 未完待续... expr 用于进行数学计算的 ...
- Tcl Tutorial 笔记 ·ubuntu命令行运行tcl 命令
ubuntu 安装tcl : sudo apt-get install tcl 在ubuntu命令行运行tcl 命令: usually via running "tclsh" or ...
- Tcl Tutorial 笔记6 ·while
目录 while 语法 while 语法 while test {body} 在这里test 作为一个表达式,if test 条件成立将执行body中的内容,如果不成立将跳出该循环. 另外注意 tes ...
- Tcl Tutorial 笔记2 · set ““ {} [] \
目录 变量赋值 set Example 替换 Substitutions "" Backslashes \ Example2 替换 Substitutions {} Example ...
- Tcl Tutorial 笔记5 ·switch
目录 switch 语法规则 example 参考资料 switch 语法规则 switch ?options? string {pattern1 {body1}?pattern2 {body2}?. ...
最新文章
- myeclipse搭建php,MyEclipse常用配置图文教程
- 学习进度条--第七周
- VC中的TRACE宏:
- Kubernetes — Calico CNI
- Java中的隐藏实现细节
- pytorch 训练过程acc_pytorch入门练手:一个简单的CNN模型
- 总结了一些指针易出错的常见问题(二)
- 微信整人假红包图片_警惕:千万别点!这些红包是假的
- linux自学笔记--vim和文本三剑客基础
- 你的 App 在 iOS 13 上被卡死了吗?
- 这个星期压力貌似比较大啊!
- 通用窗口类 Inventory Pro 2.1.2 Demo1(上)
- 联想Win10 更新系统后触控板无反应,无效
- React lazyLoad懒加载
- python3.6library 学习 1.introduction,2.built-infunction
- Spark2.1.0之初体验
- python的背景色怎么改_如何更改树的背景色
- springmvc测试类中如何引入controller与service,request,respon
- Windows安装和使用binwalk
- 微信小程序中background-attachment:fixed兼容问题