lreplace(n)

✨ lreplace(n) ✨

okay bestie, pull up a chair, because we're about to talk about lreplace β€” a Tcl Built-In Command and honestly? a total icon. πŸ’… this is the girl who swaps out pieces of your list like she's restyling an outfit. no cap.


🌸 NAME 🌸

lreplace β€” replace elements in a list with new elements πŸ¦‹


πŸŽ€ SYNOPSIS πŸŽ€

lreplace list first last ?element element ...?

πŸ’– DESCRIPTION πŸ’–

so here's the vibe: lreplace hands you back a brand new list πŸ’« β€” it doesn't mess with the original, it just makes a fresh one β€” and that new list is formed by replacing zero or more elements of list with the element arguments. glow-up achieved.

first and last are index values and they tell you the first and last elements of the range you're replacing. the way that first and last work? they're interpreted exactly the same as index values for the command string index, so you get simple index arithmetic and indices relative to the end of the list. 0 refers to the first element of the list, and end refers to the last element of the list. periodt. πŸ‘‘

now for the fun part, the edge cases, and bestie there are some slay little behaviors here:

  • if either first or last is less than zero, it's considered to refer to before the first element of the list. which means β€” plot twist β€” lreplace can prepend elements to list. 🫢
  • if either first or last indicates a position greater than the index of the last element of the list, it's treated as if it's an index one greater than the last element. which means lreplace can append elements to list. πŸ”₯
  • if last is less than first, then any specified elements get inserted into the list before the element specified by first, and no elements get deleted. literally zero deletions, we're just adding. πŸ’€ (affectionate)

and the element arguments? they specify zero or more new elements to be added to the list in place of those that were deleted. each element argument becomes a separate element of the list β€” so don't pass one big string and expect it to stay one thing, she will split it up. πŸ™ˆ

if you specify no element arguments at all? then the elements between first and last are simply deleted. that's it. gone. no replacement, just subtraction. ✨


🌈 EXAMPLES 🌈

replacing an element of a list with another:

% lreplace {a b c d e} 1 1 foo
a foo c d e

replacing two elements of a list with three:

% lreplace {a b c d e} 1 2 three more elements
a three more elements d e

deleting the last element from a list in a variable:

% set var {a b c d e}
a b c d e
% set var [lreplace $var end end]
a b c d

a procedure to delete a given element from a list:

proc lremove {listVariable value} {
    upvar 1 $listVariable var
    set idx [lsearch -exact $var $value]
    set var [lreplace $var $idx $idx]
}

appending elements to the list β€” and bestie, note this: end+2 will initially be treated as if it is 6 here, but both that and 12345 are greater than the index of the final item, so they behave identically. same energy, different numbers πŸ’…:

% set var {a b c d e}
a b c d e
% set var [lreplace $var 12345 end+2 f g h i]
a b c d e f g h i

πŸ¦‹ SEE ALSO πŸ¦‹

list(n), lappend(n), lassign(n), ledit(n), lindex(n), linsert(n), llength(n), lmap(n), lpop(n), lrange(n), lremove(n), lrepeat(n), lreverse(n), lsearch(n), lseq(n), lset(n), lsort(n), string(n)


⭐ KEYWORDS ⭐

element, list, replace


and that's the whole page, bestie β€” every flag, every edge case, every example, all of it. go forth and rearrange your lists with main character energy. πŸ’œ

this has been lreplace(n) from Tcl Built-In Commands, version 7.4 β€” mwah, ily, go write some beautiful lists πŸŽ€βœ¨πŸ«Ά

πŸ“¦ where this page comes from