1lindex(n) Tcl Built-In Commands lindex(n)
2
3
4
5______________________________________________________________________________
6
8 lindex - Retrieve an element from a list
9
11 lindex list ?index ...?
12______________________________________________________________________________
13
15 The lindex command accepts a parameter, list, which it treats as a Tcl
16 list. It also accepts zero or more indices into the list. The indices
17 may be presented either consecutively on the command line, or grouped
18 in a Tcl list and presented as a single argument.
19
20 If no indices are presented, the command takes the form:
21
22 lindex list
23
24 or
25
26 lindex list {}
27
28 In this case, the return value of lindex is simply the value of the
29 list parameter.
30
31 When presented with a single index, the lindex command treats list as a
32 Tcl list and returns the index'th element from it (0 refers to the
33 first element of the list). In extracting the element, lindex observes
34 the same rules concerning braces and quotes and backslashes as the Tcl
35 command interpreter; however, variable substitution and command substi‐
36 tution do not occur. If index is negative or greater than or equal to
37 the number of elements in value, then an empty string is returned. The
38 interpretation of each simple index value is the same as for the com‐
39 mand string index, supporting simple index arithmetic and indices rela‐
40 tive to the end of the list.
41
42 If additional index arguments are supplied, then each argument is used
43 in turn to select an element from the previous indexing operation,
44 allowing the script to select elements from sublists. The command,
45
46 lindex $a 1 2 3
47
48 or
49
50 lindex $a {1 2 3}
51
52 is synonymous with
53
54 lindex [lindex [lindex $a 1] 2] 3
55
57 Lists can be indexed into from either end:
58
59 lindex {a b c} 0
60 → a
61 lindex {a b c} 2
62 → c
63 lindex {a b c} end
64 → c
65 lindex {a b c} end-1
66 → b
67
68 Lists or sequences of indices allow selection into lists of lists:
69
70 lindex {a b c}
71 → a b c
72 lindex {a b c} {}
73 → a b c
74 lindex {{a b c} {d e f} {g h i}} 2 1
75 → h
76 lindex {{a b c} {d e f} {g h i}} {2 1}
77 → h
78 lindex {{{a b} {c d}} {{e f} {g h}}} 1 1 0
79 → g
80 lindex {{{a b} {c d}} {{e f} {g h}}} {1 1 0}
81 → g
82
83 List indices may also perform limited computation, adding or subtract‐
84 ing fixed amounts from other indices:
85
86 set idx 1
87 lindex {a b c d e f} $idx+2
88 → d
89 set idx 3
90 lindex {a b c d e f} $idx+2
91 → f
92
94 list(n), lappend(n), linsert(n), llength(n), lsearch(n), lset(n),
95 lsort(n), lrange(n), lreplace(n), string(n)
96
98 element, index, list
99
100
101
102Tcl 8.4 lindex(n)