\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#421: A Command for Iterating Over Arrays}
\date{November 28, 2012}
\author{Karl Lehenbauer, Donal K. Fellows}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#421}&\textbf{A Command for Iterating Over Arrays}\\\hline
Author&

Karl Lehenbauer \tipmail{karllehenbauer}{gmail.com}
\par
Donal K. Fellows \tipmail{dkf}{users.sf.net}
\\
Created&Wednesday, \(\text{28}^{\text{th}}\) November 2012\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.1 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes an efficient mechanism for iterating over the contents of a large array.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

Tcl currently provides three main mechanisms for iterating over the contents of an array, but none are quite perfect when dealing with a large array.
\begin{itemize}
\item{}
\textbf{array get} is simple to use (especially with a two-variable \textbf{foreach}) but requires the contents of the array to be effectively duplicated; even with the use of the Tcl\_Obj system for value reference management, this is an expensive operation.
\item{}
\textbf{array names} (with a simple \textbf{foreach}) is also relatively simple to use, but requires producing a list whose size is the same as the number of elements of the array. (This is half the size that would be required with \textbf{array get}, but can still be large.)
\item{}
\textbf{array startsearch} et al. provide a memory-efficient general iteration mechanism, but in a way that is rather difficult to use. It is also subject to significant hazards if the array is modified during iteration (a particular problem for the global \textbf{env} array, as that is regenerated on almost any read).
\end{itemize}

The authors propose that there be a new subcommand of \textbf{array} which allows for efficient iteration over an array's elements.

\section{Proposed Change}

There should be a new command, \textbf{array foreach}, that has this syntax:

\begin{quote}
\textbf{array} \textbf{foreach} \textit{arrayName} \{\textit{keyVar} \textit{valueVar}\} \textit{body}
\end{quote}

This will iterate internally over the elements of the array called \textit{arrayName} in array-iteration order (i.e., the same as that used by the other \textbf{array} subcommands), setting the variable \textit{keyVar} to the name of the element and the variable \textit{valueVar} to the content of the element before evaluating the script \textit{body}. The result will be the empty string (excepting errors, \textbf{return}, etc.) and any contained \textbf{break} and \textbf{continue} will have their normal interpretation as loop control operations.

\section{Implementation}

Not yet...

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
