lassign(n)
β¨ lassign(n) β¨
okay bestie, welcome to the lassign page from the Tcl Built-In Commands manual π this one is short, it's sweet, and it will absolutely change how you think about pulling stuff out of lists. periodt.
π NAME π
lassign β assign list elements to variables π
π¦ SYNOPSIS π¦
lassign list ?varName ...?
πΈ DESCRIPTION πΈ
So here's the vibe: this command takes the value list, treats it like, y'know, an actual list, and then hands out its elements one by one to the variables you named in the varName arguments β in order, no skipping, no shuffling. it's giving assembly line, honestly π₯
Now the messy parts, and bestie you need to know these:
- If you've got more variable names than list elements, the leftover variables get set to the empty string. not left alone, not error'd β set to
"". that's the rule π - If you've got more list elements than variables, then
lassignreturns a list of all the unassigned elements. so nothing gets lost, it just comes back atcha π«Ά
β EXAMPLES β
here's an illustration of how multiple assignment works, plus what happens when you have too few or too many elements. chef's kiss, honestly π
lassign {a b c} x y z ;# Empty return
puts $x ;# Prints "a"
puts $y ;# Prints "b"
puts $z ;# Prints "c"
lassign {d e} x y z ;# Empty return
puts $x ;# Prints "d"
puts $y ;# Prints "e"
puts $z ;# Prints ""
lassign {f g h i} x y ;# Returns "h i"
puts $x ;# Prints "f"
puts $y ;# Prints "g"
see that last one? x and y ate, and the rest (h i) got returned as a lil bonus list. iconic.
π but wait, there's more π
The lassign command has other uses. It can be used to create the analogue of the βshiftβ command in many shell languages like this:
set ::argv [lassign $::argv argumentToReadOff]
yup. pop the first thing off ::argv, stash it in argumentToReadOff, and the leftover list goes right back into ::argv. it's the shell shift glow-up and it's lowkey genius π
π SEE ALSO π
list(n)lappend(n)ledit(n)lindex(n)linsert(n)llength(n)lmap(n)lpop(n)lrange(n)lremove(n)lrepeat(n)lreplace(n)lreverse(n)lsearch(n)lseq(n)lset(n)lsort(n)
π¦ KEYWORDS π¦
assign, element, list, multiple, set, variable
and that's the whole page, bestie β no cap, every detail intact, every option present, nothing skipped π
this has been lassign(n) from the Tcl 8.5 era of the Tcl Built-In Commands manual, brought to you by your favorite Unix girlie who will never gatekeep a list command π«Άβ¨ go forth and destructure, mwah π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1992-1999 Karl Lehenbauer & Mark Diekhans
- Copyright (c) 2004 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms