<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu May 17 03:24:50 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='200'>
<header><title>Listing the Values in an Array</title><author address="mailto:donal.k.fellows@man.ac.uk">Donal K. Fellows</author><author address="mailto:dossy@panoptic.com">Dossy Shiobara</author><status type='project' state='rejected' tclversion="8.5" vote='after'>$Revision: 1.4 $</status><history></history><created day='20' month='may' year='2004' /><keyword>Tcl</keyword></header>
<abstract>This TIP proposes adding another subcommand to the <emph style="bold">array</emph> command to list the values in an array.</abstract>
<body><section title="Rationale">
<para>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 <emph style="bold">array get</emph> and dropping the <emph style="italic">name</emph> part of each pair, as can be seen from these code examples:</para>
<verbatim><vline encoding='base64'>c2V0IGxpc3Qge30=</vline><vline encoding='base64'>Zm9yZWFjaCBuYW1lIFthcnJheSBuYW1lcyBTb21lQXJyYXldIHs=</vline><vline encoding='base64'>ICAgbGFwcGVuZCBsaXN0ICRTb21lQXJyYXkoJG5hbWUp</vline><vline encoding='base64'>fQ==</vline><vline encoding='base64'></vline><vline encoding='base64'>c2V0IGxpc3Qge30=</vline><vline encoding='base64'>Zm9yZWFjaCB7bmFtZSB2YWx1ZX0gW2FycmF5IGdldCBTb21lQXJyYXldIHs=</vline><vline encoding='base64'>ICAgbGFwcGVuZCBsaXN0ICR2YWx1ZQ==</vline><vline encoding='base64'>fQ==</vline></verbatim>
<para>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.)</para>
<para>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 <emph style="bold">array</emph> for returning both the names of the array and the name/value pairs.</para>
</section>
<section title="Proposed Change">
<para>I propose to add a new subcommand to <emph style="bold">array</emph> called <emph style="bold">values</emph> with the following syntax:</para>
<quote><emph style="bold">array values</emph> <emph style="italic">arrayName</emph> ?<emph style="italic">globPattern</emph>?</quote>
<para>This returns a list of values that are contained in the array in the &quot;natural order&quot; of the values within the array. The optional <emph style="italic">globPattern</emph> argument allows for returning only some of the values in the array (those values for keys that match the pattern according to the rules of <emph style="bold">string match</emph>). The pattern will not change the ordering of the list; it only filters the result.</para>
<para>The order of the values returned shall be such that the following two <emph style="bold">foreach</emph> iterations shall behave identically (modulo traces on the array variable):</para>
<verbatim><vline encoding='base64'>Zm9yZWFjaCB7bmFtZSB2YWx1ZX0gW2FycmF5IGdldCBBbnlBcnJheSAkcGF0dGVybl0gew==</vline><vline encoding='base64'>ICAgQW55U2NyaXB0</vline><vline encoding='base64'>fQ==</vline></verbatim>
<verbatim><vline encoding='base64'>Zm9yZWFjaCBuYW1lICBbYXJyYXkgbmFtZXMgIEFueUFycmF5ICRwYXR0ZXJuXSBc</vline><vline encoding='base64'>ICAgICAgICB2YWx1ZSBbYXJyYXkgdmFsdWVzIEFueUFycmF5ICRwYXR0ZXJuXSB7</vline><vline encoding='base64'>ICAgQW55U2NyaXB0</vline><vline encoding='base64'>fQ==</vline></verbatim>
<para>Note that this implies that the resulting list from <emph style="bold">array values</emph> may well contain duplicates, and that none of the values actually returned will necessarily match the supplied pattern.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>

