ledit(n)

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

✨ 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 with lpop, lset, and lindex, 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 first or last is 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 first or last points 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 last is less than first, then any elements you specified get inserted into the list before the element specified by first, 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