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 that12345andend+2are out here living the exact same life π both bigger than the last index, both doing the identical thing. vibe check passed. β
π SEE ALSO π
list(3tcl)lappend(3tcl)lassign(3tcl)ledit(3tcl)lindex(3tcl)linsert(3tcl)llength(3tcl)lmap(3tcl)lpop(3tcl)lrange(3tcl)lremove(3tcl)lrepeat(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 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
- 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) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2001 Kevin B. Kenny <kennykb@acm.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms