lreplace(3tcl)

πŸ’– lreplace β€” the Tcl built-in glow-up for lists πŸ’–

okay so bestie, this is lreplace, a Tcl Built-In Command. She's the one who takes your list and swaps out a whole chunk of it for something new. Glow-up arc, but for elements. πŸ¦‹


✨ SYNOPSIS ✨

lreplace list first last ?element element ...?

πŸŽ€ DESCRIPTION πŸŽ€

lreplace hands you back a brand new list, made by replacing zero or more elements of list with the element arguments you gave it. no cap, the original list stays untouched β€” we're giving you a fresh one. πŸ’…

first and last are index values. they say which first and which last element of the range you're replacing. and the way they're interpreted? literally the same as index values for the command string index β€” so yes, you get simple index arithmetic and indices relative to the end of the list. 0 points at the first element of the list, and end points at the last element of the list. iconic behavior fr fr. 🌸

if either first or last is less than zero, it's considered to refer to before the first element of the list. this is the cheat code that lets lreplace prepend elements to list. 🫢

if either first or last points at 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. this is the cheat code that lets lreplace append elements to list. πŸ’–

now, plot twist: if last is less than first, then any elements you specified get inserted into the list before the element specified by first, and no elements are deleted. nothing. zero. nada. she's just adding. ✨

the element arguments are zero or more new elements to be added to the list in place of the ones that got deleted. every single element argument becomes its own separate element of the list β€” one arg, one element, no merging, periodt. and if you pass no element arguments at all? then the elements between first and last are simply deleted. bye bestie πŸ‘‹


🌈 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 note that 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:

% 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
not me pointing out that 12345 and end+2 are out here living the exact same life 😭 both bigger than the last index, both doing the identical thing. vibe check passed. βœ…

πŸ‘‘ SEE ALSO πŸ‘‘


πŸ’œ KEYWORDS πŸ’œ

  • element
  • list
  • replace

and that's the whole page, bestie!! this one came from Tcl, version 7.4, dated 7.4-era and sitting pretty under the lreplace(3tcl) name. go forth and replace your elements, you absolute main character πŸ§šπŸ’–

πŸ“¦ where this page comes from