TCL自学笔记-18 lindex函数命令

tcl脚本-lindex函数

语法:lindex list indiex

例:

set a {1 2 3 4 5}

lindex a 2

3 //返回第三个元素

或者:lindex list {}

这种情况下返回 lindex列表本身。

当只有一个单独的元素时,lindex命令返回list列表中的第index个元素。替代时元素从0开始(也就是说索引0就是指列表的第一个元素),如果index是负数或者大于列表长度就返回一个空字符串。解释器在解释每一个index值时和string index命令相同,都支持单个和多个index参数。

lindex {a b c}

→ a b cl

index {a b c} {}

→ a b c

lindex {a b c} 0

→ a

lindex {a b c} 2

→ c

lindex {a b c} end

→ c

lindex {a b c} end-1

→ b

如果指定了多个index,将会选择列表的子列表中的元素。例如:

lindex {{a b c} {d e f} {g h i}} 2 1

→ h

lindex {{a b c} {d e f} {g h i}} {2 1}

→ h

lindex {{{a b} {c d}} {{e f} {g h}}} 1 1 0

→ g

lindex {{{a b} {c d}} {{e f} {g h}}} {1 1 0}

→ g

set d [lindex $c 1 0 ]

{1 2 3} {3 2 3} {4 2 3}

(Documents) 400 % set d [lindex $c 1 0 ]

3

(Documents) 401 % set d [lindex $c 2 0 ]

4

登录后免费查看全文
立即登录
App下载
技术邻APP
工程师必备
  • 项目客服
  • 培训客服
  • 平台客服

TOP

2
1