From 4aa5d2c8459ece48ce92e83dedfd7a5c992aa7fa Mon Sep 17 00:00:00 2001 From: dkf Date: Tue, 15 Jul 2003 22:07:36 +0000 Subject: Added examples from David Welton [Patch 763312] --- ChangeLog | 4 ++++ doc/array.n | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index cbda2c2..549dfc6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2003-07-15 Donal K. Fellows + + * doc/array.n: Added some examples from David Welton [Patch 763312] + 2003-07-15 Don Porter * generic/tclCompCmds.c (TclCompileIfCmd): Prior fix of Bug 711371 diff --git a/doc/array.n b/doc/array.n index b04c89f..3839e77 100644 --- a/doc/array.n +++ b/doc/array.n @@ -5,7 +5,7 @@ '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. '\" -'\" RCS: @(#) $Id: array.n,v 1.8 2000/09/07 14:27:45 poenitz Exp $ +'\" RCS: @(#) $Id: array.n,v 1.8.18.1 2003/07/15 22:07:37 dkf Exp $ '\" .so man.macros .TH array n 8.3 Tcl "Tcl Built-In Commands" @@ -115,6 +115,10 @@ The return value is a search identifier that must be used in \fBarray nextelement\fR and \fBarray donesearch\fR commands; it allows multiple searches to be underway simultaneously for the same array. +It is currently more efficient and easier to use either the \fBarray +get\fR or \fBarray names\fR, together with \fBforeach\fR, to iterate +over all but very large arrays. See the examples below for how to do +this. .VS 8.4 .TP \fBarray statistics \fIarrayName\fR @@ -134,6 +138,55 @@ an array variable, then the command unsets the entire array. The command always returns an empty string. .VE 8.3 +.SH EXAMPLES +.CS +array set colorcount { + red 1 + green 5 + blue 4 + white 9 +} + +foreach {color count} [array get colorcount] { + puts "Color: $color Count: $count" +} + => Color: blue Count: 4 + Color: white Count: 9 + Color: green Count: 5 + Color: red Count: 1 + +foreach color [array names colorcount] { + puts "Color: $color Count: $colorcount($color)" +} + => Color: blue Count: 4 + Color: white Count: 9 + Color: green Count: 5 + Color: red Count: 1 + +foreach color [lsort [array names colorcount]] { + puts "Color: $color Count: $colorcount($color)" +} + => Color: blue Count: 4 + Color: green Count: 5 + Color: red Count: 1 + Color: white Count: 9 + +array statistics colorcount + => 4 entries in table, 4 buckets + number of buckets with 0 entries: 1 + number of buckets with 1 entries: 2 + number of buckets with 2 entries: 1 + number of buckets with 3 entries: 0 + number of buckets with 4 entries: 0 + number of buckets with 5 entries: 0 + number of buckets with 6 entries: 0 + number of buckets with 7 entries: 0 + number of buckets with 8 entries: 0 + number of buckets with 9 entries: 0 + number of buckets with 10 or more entries: 0 + average search distance for entry: 1.2 +.CE + .SH "SEE ALSO" list(n), string(n), variable(n), trace(n) -- cgit v0.12