TIP 421: A Command for Iterating Over Arrays

Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2017 Conference, Houston/TX, US, Oct 16-20
Send your abstracts to tclconference@googlegroups.com
by Aug 21.
State:		Draft
Type:		Project
Tcl-Version:	8.7
Vote:		Pending
Author:		Karl Lehenbauer <karllehenbauer@gmail.com>
Author:		Donal K. Fellows <dkf@users.sf.net>
Created:	28-Nov-2012


This TIP proposes an efficient mechanism for iterating over the contents of a large array.


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.

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

Proposed Change

There should be a new command, array foreach, that has this syntax:

array foreach arrayName {keyVar valueVar} body

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


Not yet...


This document has been placed in the public domain.