Tcl Source Code

Hex Artifact Content
Login

Artifact 38344c8a167d92073abe6b0adf5479a45ad12683:


0000: 27 5c 22 0a 27 5c 22 20 43 6f 70 79 72 69 67 68  '\".'\" Copyrigh
0010: 74 20 28 63 29 20 31 39 39 33 20 54 68 65 20 52  t (c) 1993 The R
0020: 65 67 65 6e 74 73 20 6f 66 20 74 68 65 20 55 6e  egents of the Un
0030: 69 76 65 72 73 69 74 79 20 6f 66 20 43 61 6c 69  iversity of Cali
0040: 66 6f 72 6e 69 61 2e 0a 27 5c 22 20 43 6f 70 79  fornia..'\" Copy
0050: 72 69 67 68 74 20 28 63 29 20 31 39 39 34 2d 31  right (c) 1994-1
0060: 39 39 36 20 53 75 6e 20 4d 69 63 72 6f 73 79 73  996 Sun Microsys
0070: 74 65 6d 73 2c 20 49 6e 63 2e 0a 27 5c 22 20 43  tems, Inc..'\" C
0080: 6f 70 79 72 69 67 68 74 20 28 63 29 20 32 30 30  opyright (c) 200
0090: 31 20 4b 65 76 69 6e 20 42 2e 20 4b 65 6e 6e 79  1 Kevin B. Kenny
00a0: 20 3c 6b 65 6e 6e 79 6b 62 40 61 63 6d 2e 6f 72   <[email protected]
00b0: 67 3e 2e 20 20 41 6c 6c 20 72 69 67 68 74 73 20  g>.  All rights 
00c0: 72 65 73 65 72 76 65 64 2e 0a 27 5c 22 0a 27 5c  reserved..'\".'\
00d0: 22 20 53 65 65 20 74 68 65 20 66 69 6c 65 20 22  " See the file "
00e0: 6c 69 63 65 6e 73 65 2e 74 65 72 6d 73 22 20 66  license.terms" f
00f0: 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  or information o
0100: 6e 20 75 73 61 67 65 20 61 6e 64 20 72 65 64 69  n usage and redi
0110: 73 74 72 69 62 75 74 69 6f 6e 0a 27 5c 22 20 6f  stribution.'\" o
0120: 66 20 74 68 69 73 20 66 69 6c 65 2c 20 61 6e 64  f this file, and
0130: 20 66 6f 72 20 61 20 44 49 53 43 4c 41 49 4d 45   for a DISCLAIME
0140: 52 20 4f 46 20 41 4c 4c 20 57 41 52 52 41 4e 54  R OF ALL WARRANT
0150: 49 45 53 2e 0a 27 5c 22 0a 2e 54 48 20 6c 72 65  IES..'\"..TH lre
0160: 70 6c 61 63 65 20 6e 20 37 2e 34 20 54 63 6c 20  place n 7.4 Tcl 
0170: 22 54 63 6c 20 42 75 69 6c 74 2d 49 6e 20 43 6f  "Tcl Built-In Co
0180: 6d 6d 61 6e 64 73 22 0a 2e 73 6f 20 6d 61 6e 2e  mmands"..so man.
0190: 6d 61 63 72 6f 73 0a 2e 42 53 0a 27 5c 22 20 4e  macros..BS.'\" N
01a0: 6f 74 65 3a 20 20 64 6f 20 6e 6f 74 20 6d 6f 64  ote:  do not mod
01b0: 69 66 79 20 74 68 65 20 2e 53 48 20 4e 41 4d 45  ify the .SH NAME
01c0: 20 6c 69 6e 65 20 69 6d 6d 65 64 69 61 74 65 6c   line immediatel
01d0: 79 20 62 65 6c 6f 77 21 0a 2e 53 48 20 4e 41 4d  y below!..SH NAM
01e0: 45 0a 6c 72 65 70 6c 61 63 65 20 5c 2d 20 52 65  E.lreplace \- Re
01f0: 70 6c 61 63 65 20 65 6c 65 6d 65 6e 74 73 20 69  place elements i
0200: 6e 20 61 20 6c 69 73 74 20 77 69 74 68 20 6e 65  n a list with ne
0210: 77 20 65 6c 65 6d 65 6e 74 73 0a 2e 53 48 20 53  w elements..SH S
0220: 59 4e 4f 50 53 49 53 0a 5c 66 42 6c 72 65 70 6c  YNOPSIS.\fBlrepl
0230: 61 63 65 20 5c 66 49 6c 69 73 74 20 66 69 72 73  ace \fIlist firs
0240: 74 20 6c 61 73 74 20 5c 66 52 3f 5c 66 49 65 6c  t last \fR?\fIel
0250: 65 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 20 2e 2e  ement element ..
0260: 2e 5c 66 52 3f 0a 2e 42 45 0a 2e 53 48 20 44 45  .\fR?..BE..SH DE
0270: 53 43 52 49 50 54 49 4f 4e 0a 2e 50 50 0a 5c 66  SCRIPTION..PP.\f
0280: 42 6c 72 65 70 6c 61 63 65 5c 66 52 20 72 65 74  Blreplace\fR ret
0290: 75 72 6e 73 20 61 20 6e 65 77 20 6c 69 73 74 20  urns a new list 
02a0: 66 6f 72 6d 65 64 20 62 79 20 72 65 70 6c 61 63  formed by replac
02b0: 69 6e 67 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ing one or more 
02c0: 65 6c 65 6d 65 6e 74 73 20 6f 66 0a 5c 66 49 6c  elements of.\fIl
02d0: 69 73 74 5c 66 52 20 77 69 74 68 20 74 68 65 20  ist\fR with the 
02e0: 5c 66 49 65 6c 65 6d 65 6e 74 5c 66 52 20 61 72  \fIelement\fR ar
02f0: 67 75 6d 65 6e 74 73 2e 0a 5c 66 49 66 69 72 73  guments..\fIfirs
0300: 74 5c 66 52 20 61 6e 64 20 5c 66 49 6c 61 73 74  t\fR and \fIlast
0310: 5c 66 52 20 61 72 65 20 69 6e 64 65 78 20 76 61  \fR are index va
0320: 6c 75 65 73 20 73 70 65 63 69 66 79 69 6e 67 20  lues specifying 
0330: 74 68 65 20 66 69 72 73 74 20 61 6e 64 0a 6c 61  the first and.la
0340: 73 74 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  st elements of t
0350: 68 65 20 72 61 6e 67 65 20 74 6f 20 72 65 70 6c  he range to repl
0360: 61 63 65 2e 0a 54 68 65 20 69 6e 64 65 78 20 76  ace..The index v
0370: 61 6c 75 65 73 20 5c 66 49 66 69 72 73 74 5c 66  alues \fIfirst\f
0380: 52 20 61 6e 64 20 5c 66 49 6c 61 73 74 5c 66 52  R and \fIlast\fR
0390: 20 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64   are interpreted
03a0: 0a 74 68 65 20 73 61 6d 65 20 61 73 20 69 6e 64  .the same as ind
03b0: 65 78 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68  ex values for th
03c0: 65 20 63 6f 6d 6d 61 6e 64 20 5c 66 42 73 74 72  e command \fBstr
03d0: 69 6e 67 20 69 6e 64 65 78 5c 66 52 2c 0a 73 75  ing index\fR,.su
03e0: 70 70 6f 72 74 69 6e 67 20 73 69 6d 70 6c 65 20  pporting simple 
03f0: 69 6e 64 65 78 20 61 72 69 74 68 6d 65 74 69 63  index arithmetic
0400: 20 61 6e 64 20 69 6e 64 69 63 65 73 20 72 65 6c   and indices rel
0410: 61 74 69 76 65 20 74 6f 20 74 68 65 0a 65 6e 64  ative to the.end
0420: 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 0a 30 20   of the list..0 
0430: 72 65 66 65 72 73 20 74 6f 20 74 68 65 20 66 69  refers to the fi
0440: 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  rst element of t
0450: 68 65 0a 6c 69 73 74 2c 20 61 6e 64 20 5c 66 42  he.list, and \fB
0460: 65 6e 64 5c 66 52 20 72 65 66 65 72 73 20 74 6f  end\fR refers to
0470: 20 74 68 65 20 6c 61 73 74 20 65 6c 65 6d 65 6e   the last elemen
0480: 74 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 0a 49  t of the list..I
0490: 66 20 5c 66 49 6c 69 73 74 5c 66 52 20 69 73 20  f \fIlist\fR is 
04a0: 65 6d 70 74 79 2c 20 74 68 65 6e 20 5c 66 49 66  empty, then \fIf
04b0: 69 72 73 74 5c 66 52 20 61 6e 64 20 5c 66 49 6c  irst\fR and \fIl
04c0: 61 73 74 5c 66 52 20 61 72 65 20 69 67 6e 6f 72  ast\fR are ignor
04d0: 65 64 2e 0a 2e 50 50 0a 49 66 20 5c 66 49 66 69  ed...PP.If \fIfi
04e0: 72 73 74 5c 66 52 20 69 73 20 6c 65 73 73 20 74  rst\fR is less t
04f0: 68 61 6e 20 7a 65 72 6f 2c 20 69 74 20 69 73 20  han zero, it is 
0500: 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 72 65  considered to re
0510: 66 65 72 20 74 6f 20 62 65 66 6f 72 65 20 74 68  fer to before th
0520: 65 0a 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20  e.first element 
0530: 6f 66 20 74 68 65 20 6c 69 73 74 2e 20 20 46 6f  of the list.  Fo
0540: 72 20 6e 6f 6e 2d 65 6d 70 74 79 20 6c 69 73 74  r non-empty list
0550: 73 2c 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 69  s, the element i
0560: 6e 64 69 63 61 74 65 64 0a 62 79 20 5c 66 49 66  ndicated.by \fIf
0570: 69 72 73 74 5c 66 52 20 6d 75 73 74 20 65 78 69  irst\fR must exi
0580: 73 74 20 6f 72 20 5c 66 49 66 69 72 73 74 5c 66  st or \fIfirst\f
0590: 52 20 6d 75 73 74 20 69 6e 64 69 63 61 74 65 20  R must indicate 
05a0: 62 65 66 6f 72 65 20 74 68 65 0a 73 74 61 72 74  before the.start
05b0: 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 0a 2e 50   of the list...P
05c0: 50 0a 49 66 20 5c 66 49 6c 61 73 74 5c 66 52 20  P.If \fIlast\fR 
05d0: 69 73 20 6c 65 73 73 20 74 68 61 6e 20 5c 66 49  is less than \fI
05e0: 66 69 72 73 74 5c 66 52 2c 20 74 68 65 6e 20 61  first\fR, then a
05f0: 6e 79 20 73 70 65 63 69 66 69 65 64 20 65 6c 65  ny specified ele
0600: 6d 65 6e 74 73 0a 77 69 6c 6c 20 62 65 20 69 6e  ments.will be in
0610: 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20  serted into the 
0620: 6c 69 73 74 20 62 65 66 6f 72 65 20 74 68 65 20  list before the 
0630: 70 6f 69 6e 74 20 73 70 65 63 69 66 69 65 64 20  point specified 
0640: 62 79 20 5c 66 49 66 69 72 73 74 5c 66 52 0a 77  by \fIfirst\fR.w
0650: 69 74 68 20 6e 6f 20 65 6c 65 6d 65 6e 74 73 20  ith no elements 
0660: 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 2e  being deleted...
0670: 50 50 0a 54 68 65 20 5c 66 49 65 6c 65 6d 65 6e  PP.The \fIelemen
0680: 74 5c 66 52 20 61 72 67 75 6d 65 6e 74 73 20 73  t\fR arguments s
0690: 70 65 63 69 66 79 20 7a 65 72 6f 20 6f 72 20 6d  pecify zero or m
06a0: 6f 72 65 20 6e 65 77 20 61 72 67 75 6d 65 6e 74  ore new argument
06b0: 73 20 74 6f 0a 62 65 20 61 64 64 65 64 20 74 6f  s to.be added to
06c0: 20 74 68 65 20 6c 69 73 74 20 69 6e 20 70 6c 61   the list in pla
06d0: 63 65 20 6f 66 20 74 68 6f 73 65 20 74 68 61 74  ce of those that
06e0: 20 77 65 72 65 20 64 65 6c 65 74 65 64 2e 0a 45   were deleted..E
06f0: 61 63 68 20 5c 66 49 65 6c 65 6d 65 6e 74 5c 66  ach \fIelement\f
0700: 52 20 61 72 67 75 6d 65 6e 74 20 77 69 6c 6c 20  R argument will 
0710: 62 65 63 6f 6d 65 20 61 20 73 65 70 61 72 61 74  become a separat
0720: 65 20 65 6c 65 6d 65 6e 74 20 6f 66 0a 74 68 65  e element of.the
0730: 20 6c 69 73 74 2e 20 20 49 66 20 6e 6f 20 5c 66   list.  If no \f
0740: 49 65 6c 65 6d 65 6e 74 5c 66 52 20 61 72 67 75  Ielement\fR argu
0750: 6d 65 6e 74 73 20 61 72 65 20 73 70 65 63 69 66  ments are specif
0760: 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20 65 6c  ied, then the el
0770: 65 6d 65 6e 74 73 0a 62 65 74 77 65 65 6e 20 5c  ements.between \
0780: 66 49 66 69 72 73 74 5c 66 52 20 61 6e 64 20 5c  fIfirst\fR and \
0790: 66 49 6c 61 73 74 5c 66 52 20 61 72 65 20 73 69  fIlast\fR are si
07a0: 6d 70 6c 79 20 64 65 6c 65 74 65 64 2e 20 20 49  mply deleted.  I
07b0: 66 20 5c 66 49 6c 69 73 74 5c 66 52 0a 69 73 20  f \fIlist\fR.is 
07c0: 65 6d 70 74 79 2c 20 61 6e 79 20 5c 66 49 65 6c  empty, any \fIel
07d0: 65 6d 65 6e 74 5c 66 52 20 61 72 67 75 6d 65 6e  ement\fR argumen
07e0: 74 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20  ts are added to 
07f0: 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c  the end of the l
0800: 69 73 74 2e 0a 2e 53 48 20 45 58 41 4d 50 4c 45  ist...SH EXAMPLE
0810: 53 0a 2e 50 50 0a 52 65 70 6c 61 63 69 6e 67 20  S..PP.Replacing 
0820: 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20  an element of a 
0830: 6c 69 73 74 20 77 69 74 68 20 61 6e 6f 74 68 65  list with anothe
0840: 72 3a 0a 2e 50 50 0a 2e 43 53 0a 25 20 5c 66 42  r:..PP..CS.% \fB
0850: 6c 72 65 70 6c 61 63 65 5c 66 52 20 7b 61 20 62  lreplace\fR {a b
0860: 20 63 20 64 20 65 7d 20 31 20 31 20 66 6f 6f 0a   c d e} 1 1 foo.
0870: 61 20 66 6f 6f 20 63 20 64 20 65 0a 2e 43 45 0a  a foo c d e..CE.
0880: 2e 50 50 0a 52 65 70 6c 61 63 69 6e 67 20 74 77  .PP.Replacing tw
0890: 6f 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20  o elements of a 
08a0: 6c 69 73 74 20 77 69 74 68 20 74 68 72 65 65 3a  list with three:
08b0: 0a 2e 50 50 0a 2e 43 53 0a 25 20 5c 66 42 6c 72  ..PP..CS.% \fBlr
08c0: 65 70 6c 61 63 65 5c 66 52 20 7b 61 20 62 20 63  eplace\fR {a b c
08d0: 20 64 20 65 7d 20 31 20 32 20 74 68 72 65 65 20   d e} 1 2 three 
08e0: 6d 6f 72 65 20 65 6c 65 6d 65 6e 74 73 0a 61 20  more elements.a 
08f0: 74 68 72 65 65 20 6d 6f 72 65 20 65 6c 65 6d 65  three more eleme
0900: 6e 74 73 20 64 20 65 0a 2e 43 45 0a 2e 50 50 0a  nts d e..CE..PP.
0910: 44 65 6c 65 74 69 6e 67 20 74 68 65 20 6c 61 73  Deleting the las
0920: 74 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d 20 61  t element from a
0930: 20 6c 69 73 74 20 69 6e 20 61 20 76 61 72 69 61   list in a varia
0940: 62 6c 65 3a 0a 2e 50 50 0a 2e 43 53 0a 25 20 73  ble:..PP..CS.% s
0950: 65 74 20 76 61 72 20 7b 61 20 62 20 63 20 64 20  et var {a b c d 
0960: 65 7d 0a 61 20 62 20 63 20 64 20 65 0a 25 20 73  e}.a b c d e.% s
0970: 65 74 20 76 61 72 20 5b 5c 66 42 6c 72 65 70 6c  et var [\fBlrepl
0980: 61 63 65 5c 66 52 20 24 76 61 72 20 65 6e 64 20  ace\fR $var end 
0990: 65 6e 64 5d 0a 61 20 62 20 63 20 64 0a 2e 43 45  end].a b c d..CE
09a0: 0a 2e 50 50 0a 41 20 70 72 6f 63 65 64 75 72 65  ..PP.A procedure
09b0: 20 74 6f 20 64 65 6c 65 74 65 20 61 20 67 69 76   to delete a giv
09c0: 65 6e 20 65 6c 65 6d 65 6e 74 20 66 72 6f 6d 20  en element from 
09d0: 61 20 6c 69 73 74 3a 0a 2e 50 50 0a 2e 43 53 0a  a list:..PP..CS.
09e0: 70 72 6f 63 20 6c 72 65 6d 6f 76 65 20 7b 6c 69  proc lremove {li
09f0: 73 74 56 61 72 69 61 62 6c 65 20 76 61 6c 75 65  stVariable value
0a00: 7d 20 7b 0a 20 20 20 20 75 70 76 61 72 20 31 20  } {.    upvar 1 
0a10: 24 6c 69 73 74 56 61 72 69 61 62 6c 65 20 76 61  $listVariable va
0a20: 72 0a 20 20 20 20 73 65 74 20 69 64 78 20 5b 6c  r.    set idx [l
0a30: 73 65 61 72 63 68 20 2d 65 78 61 63 74 20 24 76  search -exact $v
0a40: 61 72 20 24 76 61 6c 75 65 5d 0a 20 20 20 20 73  ar $value].    s
0a50: 65 74 20 76 61 72 20 5b 5c 66 42 6c 72 65 70 6c  et var [\fBlrepl
0a60: 61 63 65 5c 66 52 20 24 76 61 72 20 24 69 64 78  ace\fR $var $idx
0a70: 20 24 69 64 78 5d 0a 7d 0a 2e 43 45 0a 2e 53 48   $idx].}..CE..SH
0a80: 20 22 53 45 45 20 41 4c 53 4f 22 0a 6c 69 73 74   "SEE ALSO".list
0a90: 28 6e 29 2c 20 6c 61 70 70 65 6e 64 28 6e 29 2c  (n), lappend(n),
0aa0: 20 6c 69 6e 64 65 78 28 6e 29 2c 20 6c 69 6e 73   lindex(n), lins
0ab0: 65 72 74 28 6e 29 2c 20 6c 6c 65 6e 67 74 68 28  ert(n), llength(
0ac0: 6e 29 2c 20 6c 73 65 61 72 63 68 28 6e 29 2c 0a  n), lsearch(n),.
0ad0: 6c 73 65 74 28 6e 29 2c 20 6c 72 61 6e 67 65 28  lset(n), lrange(
0ae0: 6e 29 2c 20 6c 73 6f 72 74 28 6e 29 2c 0a 73 74  n), lsort(n),.st
0af0: 72 69 6e 67 28 6e 29 0a 2e 53 48 20 4b 45 59 57  ring(n)..SH KEYW
0b00: 4f 52 44 53 0a 65 6c 65 6d 65 6e 74 2c 20 6c 69  ORDS.element, li
0b10: 73 74 2c 20 72 65 70 6c 61 63 65 0a              st, replace.