lassign(3tcl)
β¨ 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?
lassignreturns 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:
list(3tcl)lappend(3tcl)ledit(3tcl)lindex(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)
π 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
- 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) 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