ledit(3tcl)
π ledit(3tcl) π
okay bestie, so this one lives in the Tcl Built-In Commands family, and it's genuinely one of the cutest list tools out there. it's called ledit, and no cap, it does exactly what it says on the tin.
β¨ NAME β¨
ledit β Replace elements of a list stored in variable
πΈ SYNOPSIS πΈ
ledit listVar first last ?value value ...?
π¦ DESCRIPTION π¦
okay so here's the tea. this command goes and fetches the list value that's sitting in the variable listVar, then it swaps out the elements in the range from first to last (inclusive, both ends, we're not playing favorites) with whatever value arguments you handed it. then the glow-up list gets stored right back into listVar and returned as the result of the command. it's giving two-for-one.
the first and last arguments are index values β they tell you the first and last elements of the range you're replacing. and the way they get interpreted? same energy as index values for the command string index, so you get simple index arithmetic and indices relative to the end of the list. index 0 means the very first element of the list, and end means the last element of the list.
heads up bestie π β unlike withlpop,lset, andlindex, indices into sublists are not supported here. so don't try it.
if either first or last is less than zero, it's treated as referring to the position before the first element of the list. this is literally the whole trick that lets you prepend stuff. β¨
if either first or last points to a position greater than the index of the last element, it's treated as if it's an index one greater than the last element. that's how you append. π
and if last is less than first? then any elements you specified just get inserted into the list before the element specified by first, and nothing gets deleted at all. it's an insert moment, not a replace moment.
now, the value arguments. they specify zero or more new elements to drop into the list in place of the ones that got deleted. each value argument becomes its own separate element of the list β they don't get smooshed together, okay? and if you specify no value arguments at all, the elements between first and last are simply deleted. poof. gone. π
π EXAMPLES π
Prepend to a list.
set lst {c d e f g}
β c d e f g
ledit lst -1 -1 a b
β a b c d e f g
Append to the list.
ledit lst end+1 end+1 h i
β a b c d e f g h i
Delete third and fourth elements.
ledit lst 2 3
β a b e f g h i
Replace two elements with three.
ledit lst 2 3 x y z
β a b x y z g h i
set lst
β a b x y z g h i
π SEE ALSO π
list(3tcl)lappend(3tcl)lassign(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)string(3tcl)
π§ KEYWORDS π§
element, list, replace
and that's the whole page, bestie π«Ά this one comes to you from Tcl 9.0 β yes, that's the version and date energy right there in the footer, and honestly? iconic. go forth and edit your lists. mwah π
π¦ 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) 2022 Ashok P. Nadkarni <apnmbx-public@yahoo.com>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms