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
| Argument | Type | Default | Description |
|---|---|---|---|
| LIST | list | required | The list. |
| FIRST | index | required | The index of the first element to replace. |
| LAST | index | required | The index of the last element to replace. |
| ELEMENT | any value | none | A 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
- A bad index has
-errorcode NONE, where Tcl 8.6.16 and 9.0.4 giveTCL VALUE INDEX(issue 658).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1