lreplace

Gives a list with a range of elements replaced by new elements.

lreplace LIST FIRST LAST ?ELEMENT ...?

Description

Gives a new list: LIST with the elements from FIRST to LAST, both included, replaced by the ELEMENT arguments. With no ELEMENT the range goes away. The list of a variable does not change.

A FIRST below 0 counts as 0, and a LAST past the end counts as the last element. When LAST is before FIRST, no element goes away and the new elements go in before FIRST. A FIRST past the end puts the new elements at the end.

Arguments

ArgumentTypeDefaultDescription
LISTlistrequiredThe list.
FIRSTindexrequiredThe index of the first element to replace.
LASTindexrequiredThe index of the last element to replace.
ELEMENTany valuenoneA new element.

Returns

The new list.

Examples

% lreplace {a b c d} 1 2 x
a x d

% lreplace {a b c d} 1 2
a d

% lreplace {a b c d} 2 1 x
a b x c d

% lreplace {a b c d} 3 10 x
a b c x

% lreplace {a b} 5 6 x
a b x

Errors

% lreplace {a b} x 1
bad index "x": must be integer?[+-]integer? or end?[+-]integer?
-errorcode NONE

% lreplace {a b} 0
wrong # args: should be "lreplace list first last ?element ...?"
-errorcode TCL WRONGARGS

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also