lindex(3tcl)
β¨ NAME β¨
lindex β bestie, it grabs you an element out of a list π
πΈ SYNOPSIS πΈ
lindex list ?index ...?
π DESCRIPTION π
So lindex takes one parameter, list, and it's likeβ¦ it treats that thing as a Tcl list, okay? It also takes zero or more indices into the list. And here's the cute part: you can hand those indices over one after another right there on the command line, or you can group them all up in a Tcl list and pass them as a single argument. Both serve. π
If you present no indices at all, the command looks like:
lindex list
or
lindex list {}
and in that case the return value is justβ¦ literally the value of the list parameter. Nothing extra, no notes. π«Ά
When you hand it a single index, lindex treats list as a Tcl list and gives you back the index'th element from it (and yes, 0 means the first element β we're zero-indexed girlies in this house). While it's pulling that element out, lindex follows the exact same rules about braces and quotes and backslashes as the Tcl command interpreter does β however, variable substitution and command substitution do not happen here. Not the vibe. If index is negative, or greater than or equal to the number of elements in value, then you get back an empty string. Sorry bestie π. The way each simple index value gets interpreted is the same as for the command string index, so it supports simple index arithmetic and indices relative to the end of the list. Iconic. β¨
If you supply additional index arguments, then each argument gets used in turn to select an element from the previous indexing operation β which is how your script reaches into sublists. Like:
lindex $a 1 2 3
or
lindex $a {1 2 3}
is synonymous with
lindex [lindex [lindex $a 1] 2] 3
same energy, less typing. π
π EXAMPLES π
Lists can be indexed into from either end, which is so fun of them:
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
Lists or sequences of indices let you select into lists of lists (lists in lists in lists, it's giving inception π¦):
lindex {a b c}
β a b c
lindex {a b c} {}
β a b c
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
List indices may also do limited computation, adding or subtracting fixed amounts from other indices. Math but make it fashion:
set idx 1
lindex {a b c d e f} $idx+2
β d
set idx 3
lindex {a b c d e f} $idx+2
β f
π SEE ALSO π
list(3tcl)lappend(3tcl)lassign(3tcl)ledit(3tcl)linsert(3tcl)llength(3tcl)lmap(3tcl)lpop(3tcl)lrange(3tcl)lremove(3tcl)lrepeat(3tcl)lreplace(3tcl)lreverse(3tcl)lsearch(3tcl)lseq(3tcl)lset(3tcl)lsort(3tcl)string(3tcl)
π KEYWORDS π
element, index, list
okay that's the whole page, mwah π«Ά this was Tcl 8.4, lindex(3tcl), and no cap it's been a pleasure serving cunt with you πβ¨π
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2001 Kevin B. Kenny <kennykb@acm.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms