lindex(3tcl)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ 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 πŸ‘‘


πŸ’œ 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