| Donal K. Fellows 2004-05-24, 1:33 pm |
|
TIP #200: LISTING THE VALUES IN AN ARRAY
========================================
==
Version: $Revision: 1.1 $
Author: Donal K. Fellows <donal.k.fellows_at_man.ac.uk>
Dossy Shiobara <dossy_at_panoptic.com>
State: Draft
Type: Project
Tcl-Version: 8.5
Vote: Pending
Created: Thursday, 20 May 2004
URL: http://purl.org/tcl/tip/200.html
WebEdit: http://purl.org/tcl/tip/edit/200
Post-History:
-------------------------------------------------------------------------
ABSTRACT
==========
This TIP proposes adding another subcommand to the *array* command to
list the values in an array.
RATIONALE
===========
Currently, the easiest ways of building a list of all values within an
array involve either iterating over the names of the array elements and
then reading the value at each step, or iterating over the result of
*array get* and dropping the /name/ part of each pair, as can be seen
from these code examples:
set list {}
foreach name [array names SomeArray] {
lappend list $SomeArray($name)
}
set list {}
foreach {name value} [array get SomeArray] {
lappend list $value
}
Neither of these is especially efficient, since the first does a very
large number of array reads, and the second builds a very large
intermediate list (particularly if you subsequently want to filter the
values to select a subset of them.)
It would be better if the functionality was added directly to the Tcl
core, especially as the conceptual overhead would be very low as there
are already subcommands of *array* for returning both the names of the
array and the name/value pairs.
PROPOSED CHANGE
=================
I propose to add a new subcommand to *array* called *values* with the
following syntax:
*array values* /arrayName/ ?/globPattern/?
This returns a list of values that are contained in the array in the
"natural order" of the values within the array. The optional
/globPattern/ argument allows for returning only some of the values in
the array (those that match the pattern according to the rules of
*string match*, of course). The pattern will not change the ordering of
the list; it only filters the result.
The order of the values returned shall be such that the following two
*foreach* iterations shall behave identically (modulo traces on the
array variable):
foreach {name value} [array get AnyArray] {
AnyScript
}
foreach name [array names AnyArray] value [array values AnyArray] {
AnyScript
}
Note that this implies that the resulting list from *array values* may
well contain duplicates.
COPYRIGHT
===========
This document has been placed in the public domain.
-------------------------------------------------------------------------
TIP AutoGenerator - written by Donal K. Fellows
[[Send Tcl/Tk announcements to tcl-announce@mitchell.org
Announcements archived at http://groups.yahoo.com/group/tcl_announce/
Send administrivia to tcl-announce-request@mitchell.org
Tcl/Tk at http://tcl.tk/ ]]
|