lassign(n)

πŸ“¦
version 9.0.3 Β· 3 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

✨ 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 lassign returns 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 πŸ’–


πŸ¦‹ 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