| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" Copyright (c) 2001 Kevin B. Kenny <[email protected]>. All rights reserved. |
| 5 |
'\" |
| 6 |
'\" See the file "license.terms" for information on usage and redistribution |
| 7 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 8 |
'\" |
| 9 |
.TH lreplace n 7.4 Tcl "Tcl Built-In Commands" |
| 10 |
.so man.macros |
| 11 |
.BS |
| 12 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 13 |
.SH NAME |
| 14 |
lreplace \- Replace elements in a list with new elements |
| 15 |
.SH SYNOPSIS |
| 16 |
\fBlreplace \fIlist first last \fR?\fIelement element ...\fR? |
| 17 |
.BE |
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
\fBlreplace\fR returns a new list formed by replacing zero or more elements of |
| 21 |
\fIlist\fR with the \fIelement\fR arguments. |
| 22 |
\fIfirst\fR and \fIlast\fR are index values specifying the first and |
| 23 |
last elements of the range to replace. |
| 24 |
The index values \fIfirst\fR and \fIlast\fR are interpreted |
| 25 |
the same as index values for the command \fBstring index\fR, |
| 26 |
supporting simple index arithmetic and indices relative to the |
| 27 |
end of the list. |
| 28 |
0 refers to the first element of the |
| 29 |
list, and \fBend\fR refers to the last element of the list. |
| 30 |
.PP |
| 31 |
If either \fIfirst\fR or \fIlast\fR is less than zero, it is considered |
| 32 |
to refer to before the first element of the list. This allows \fBlreplace\fR |
| 33 |
to prepend elements to \fIlist\fR. |
| 34 |
.VS TIP505 |
| 35 |
If either \fIfirst\fR or \fIlast\fR indicates a position greater than the |
| 36 |
index of the last element of the list, it is treated as if it is an |
| 37 |
index one greater than the last element. This allows \fBlreplace\fR to |
| 38 |
append elements to \fIlist\fR. |
| 39 |
.VE TIP505 |
| 40 |
.PP |
| 41 |
If \fIlast\fR is less than \fIfirst\fR, then any specified elements |
| 42 |
will be inserted into the list before the element specified by \fIfirst\fR |
| 43 |
with no elements being deleted. |
| 44 |
.PP |
| 45 |
The \fIelement\fR arguments specify zero or more new elements to |
| 46 |
be added to the list in place of those that were deleted. |
| 47 |
Each \fIelement\fR argument will become a separate element of |
| 48 |
the list. If no \fIelement\fR arguments are specified, then the elements |
| 49 |
between \fIfirst\fR and \fIlast\fR are simply deleted. |
| 50 |
.SH EXAMPLES |
| 51 |
.PP |
| 52 |
Replacing an element of a list with another: |
| 53 |
.PP |
| 54 |
.CS |
| 55 |
% \fBlreplace\fR {a b c d e} 1 1 foo |
| 56 |
a foo c d e |
| 57 |
.CE |
| 58 |
.PP |
| 59 |
Replacing two elements of a list with three: |
| 60 |
.PP |
| 61 |
.CS |
| 62 |
% \fBlreplace\fR {a b c d e} 1 2 three more elements |
| 63 |
a three more elements d e |
| 64 |
.CE |
| 65 |
.PP |
| 66 |
Deleting the last element from a list in a variable: |
| 67 |
.PP |
| 68 |
.CS |
| 69 |
% set var {a b c d e} |
| 70 |
a b c d e |
| 71 |
% set var [\fBlreplace\fR $var end end] |
| 72 |
a b c d |
| 73 |
.CE |
| 74 |
.PP |
| 75 |
A procedure to delete a given element from a list: |
| 76 |
.PP |
| 77 |
.CS |
| 78 |
proc lremove {listVariable value} { |
| 79 |
upvar 1 $listVariable var |
| 80 |
set idx [lsearch -exact $var $value] |
| 81 |
set var [\fBlreplace\fR $var $idx $idx] |
| 82 |
} |
| 83 |
.CE |
| 84 |
.PP |
| 85 |
.VS TIP505 |
| 86 |
Appending elements to the list; note that \fBend+2\fR will initially |
| 87 |
be treated as if it is \fB6\fR here, but both that and \fB12345\fR are greater |
| 88 |
than the index of the final item so they behave identically: |
| 89 |
.PP |
| 90 |
.CS |
| 91 |
% set var {a b c d e} |
| 92 |
a b c d e |
| 93 |
% set var [\fBlreplace\fR $var 12345 end+2 f g h i] |
| 94 |
a b c d e f g h i |
| 95 |
.CE |
| 96 |
.VE TIP505 |
| 97 |
.SH "SEE ALSO" |
| 98 |
list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n), |
| 99 |
lset(n), lrange(n), lsort(n), |
| 100 |
string(n) |
| 101 |
.SH KEYWORDS |
| 102 |
element, list, replace |
| 103 |
.\" Local variables: |
| 104 |
.\" mode: nroff |
| 105 |
.\" fill-column: 78 |
| 106 |
.\" End: |