1. 程式人生 > >tcl/tk參考——列表操作lindex

tcl/tk參考——列表操作lindex

.

.

名稱

lindex - 從列表中獲得一個元素

語法

lindex list ?index...?

描述

lindex命令接受一個引數列表list,可以接受0個或者多個index引數,在多個引數的情況下,引數可以是單獨的一次排列,也可以是在一個列表當中。

如果不指定index引數:

lindex list
或者
lindex list {}
這種情況下返回lindex列表本身。

當只有一個單獨的元素時,lindex命令返回list列表中的第index個元素。替代時元素從0開始(也就是說索引0就是指列表的第一個元素),如果index是負數或者大於列表長度就返回一個空字串。直譯器在解釋每一個index

值時和string index命令相同,都支援單個和多個index引數。

如果指定了多個index,將會選擇列表的子列表中的元素。例如

lindex $a 1 2 3
或者
lindex $a {1 2 3}
與下面的命令相同
lindex [lindex [lindex $a 1] 2] 3

示例

lindex {a b c}
       a b c
lindex {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
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