lassign(3tcl)

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

✨ lassign β€” bestie edition ✨

ok so you know lassign from Tcl Built-In Commands? yeah. that one. sit down, we're doing this πŸ’…


🌸 NAME 🌸

lassign β€” assigns list elements to variables. that's it. that's the whole job. she's simple, she's iconic, she eats. πŸ‘‘


πŸŽ€ SYNOPSIS πŸŽ€

lassign list ?varName ...?

like, give her a list and then however many varNames you want (or none! she doesn't judge 🫢).


πŸ’– DESCRIPTION πŸ’–

okay so here's the vibe: this command takes the value list, treats it like 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 favorites. ✨

and then there's the messy part, because lists and variable counts never match up, do they 😭:

  • more variable names than list elements? the leftover variables just get set to the empty string. they showed up to the party and nobody came. empty string energy.
  • more list elements than variables? lassign returns a list of all the unassigned elements. she's not gonna just lose them, she's not a monster. πŸ’€

🌈 EXAMPLES 🌈

here's a lil demo of how the multiple assignment thing works, plus what happens when you've got too few or too many elements. yes, we're doing all three cases, because she contains multitudes. πŸ¦‹

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 how in the last one x and y get f and g, and h i comes back to you like a little gift? that's the return value, bestie. 🎁

but wait, there's more β€” lassign has other uses too. she's multifaceted. πŸ‘‘ you can use her to build the analogue of the shift command that lots of shell languages have, like this:

set ::argv [lassign $::argv argumentToReadOff]

popping off the front of your args and reassigning the rest. chef's kiss. no cap. πŸ”₯


πŸ‘€ SEE ALSO πŸ‘€

go read her friends, they're all lovely:


πŸ’œ KEYWORDS πŸ’œ

assign, element, list, multiple, set, variable


and that's the whole page, babe πŸŽ€ she's from Tcl 8.5, she's a lassign(3tcl) girlie, and she will never let a list element go unassigned. slay, go assign something. πŸ’–βœ¨

πŸ“¦ where this page comes from