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
firstorlastis less than zero, it's considered to refer to before the first element of the list. which means β plot twist βlreplacecan prepend elements tolist. π«Ά - if either
firstorlastindicates 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 meanslreplacecan append elements tolist. π₯ - if
lastis less thanfirst, then any specified elements get inserted into the list before the element specified byfirst, 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
- 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) 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