ledit(n)
β¨ NAME β¨
ok so bestie, this is ledit β the Tcl built-in that replaces elements of a list stored in a variable. that's it, that's the vibe π
π SYNOPSIS π
ledit listVar first last ?value value ...?
π DESCRIPTION π
okay so here's the tea π« β this command goes and fetches the list value sitting in the variable listVar, then swaps out the elements in the range from index first to index last (both ends included, no sneaky exclusions bestie), and drops the value arguments in their place. then it stores the resulting list right back into listVar and hands it back to you as the result of the command. slay β¨
first and last are index values β they point at the first and last elements of the range you wanna replace. they get interpreted exactly the same way as index values for the command string index, so yes, simple index arithmetic is on the menu, and so are indices relative to the end of the list. index 0 refers to the first element of the list, and end refers to the last element of the list. iconic.
heads up bestie β unlike withlpop,lset, andlindex, indices into sublists are not supported. no cap, that's just how it is π
now for the fun edge cases, because there are several and they're lowkey the whole point:
- if either
firstorlastis less than zero, it's considered to refer to the position before the first element of the list. this is how you get elements prepended π - if either
firstorlastpoints to a position greater than the index of the last element of the list, it gets treated as if it were an index one greater than the last element. this is how you get elements appended π¦ - if
lastis less thanfirst, then any elements you specified get inserted into the list before the element specified byfirst, and literally nothing gets deleted. it's giving pure insertion energy β¨
the value arguments are zero or more new elements to be added to the list in place of the ones that got deleted. each value argument becomes its own separate element β one argument, one element, no merging, no grouping, periodt π
. and if you pass no value arguments at all? then the elements between first and last are just... simply deleted. gone. bye π
π EXAMPLES π
Prepend to a list. (putting stuff at the front, main character behaviour π)
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. (and now we're at the back π)
ledit lst end+1 end+1 h i
β a b c d e f g h i
Delete third and fourth elements. (not me just removing them π)
ledit lst 2 3
β a b e f g h i
Replace two elements with three. (glow up, two became 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(n), lappend(n), lassign(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), string(n)
the whole list-manipulation friend group fr fr π«Ά
π KEYWORDS π
element, list, replace
that's the whole page bestie, you're so ready π
β¨ this was Tcl 9.0, ledit(n) β and yes, the ledit(n) in that header is the exact same page you just read, go off πΈπ
π¦ 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
- 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