:TITLE: More Array Commands - Iterating through array ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl23.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: There are two methods for iterating through the contents of an associative array.

In a loop, the foreach mechanism is the simplest. If the code iterating through the array is distributed through several proc's, then the array startsearch, array nextelement, etc. commands are more appropriate.

array startsearch arrayName
Initializes arrayName for use with the iterating commands, and returns the searchID token that must be passed to those commands. Multiple iterations may be in progress for a single array at the same time provided that there is a separate searchID for each iteration.
array nextelement arrayName searchID
Returns the index of the next element in arrayName. If there are no more elements in arrayName, then an empty string is returned.
array anymore arrayName searchID
Returns a 1 if there are still elements in arrayName to be returned by nextelement. Otherwise, it returns 0.
array donesearch arrayName searchID
Destroys the state information associated with the iteration commands. This command should be executed when an iteration is complete.

Note that if you add or delete an array element while an iteration is in progress, then that iteration is no longer valid. After such a modification ot the array, array nextelement will return an empty string, and array anymore will return a zero. :TEXT_END: :LESSON_TEXT_START_LEVEL 1: There are two methods for iterating through the contents of an associative array.

The foreach mechanism is the simplest method of iterating through an array. However, if the code iterating through the array is distributed through several proc's, then the array startsearch, array nextelement, etc. commands are more appropriate.

array startsearch arrayName
Initializes arrayName for use with the iterating commands, and returns the searchID token that must be passed to those commands. Multiple iterations may be in progress for a single array at the same time provided that there is a separate searchID for each iteration.
array nextelement arrayName searchID
Returns the index of the next element in arrayName. If there are no more elements in arrayName, then an empty string is returned.
array anymore arrayName searchID
Returns a 1 if there are still elements in arrayName to be returned by nextelement. Otherwise, it returns 0.
array donesearch arrayName searchID
Destroys the state information associated with the iteration commands. This command should be executed when an iteration is complete.

Note that if you add or delete an array element while an iteration is in progress, then that iteration is no longer valid. After such a modification ot the array, array nextelement will return an empty string, and array anymore will return a zero. :TEXT_END: :LESSON_TEXT_START_LEVEL 2: There are two methods for iterating through the contents of an associative array.

In a loop, the foreach mechanism is the simplest. If the code iterating through the array is distributed through several proc's, then the array startsearch, array nextelement, etc. commands are more appropriate.

array startsearch arrayName
Initializes arrayName for use with the iterating commands, and returns the searchID token that must be passed to those commands. Multiple iterations may be in progress for a single array at the same time provided that there is a separate searchID for each iteration.
array nextelement arrayName searchID
Returns the index of the next element in arrayName. If there are no more elements in arrayName, then an empty string is returned.
array anymore arrayName searchID
Returns a 1 if there are still elements in arrayName to be returned by nextelement. Otherwise, it returns 0.
array donesearch arrayName searchID
Destroys the state information associated with the iteration commands. This command should be executed when an iteration is complete.

Note that if you add or delete an array element while an iteration is in progress, then that iteration is no longer valid. After such a modification ot the array, array nextelement will return an empty string, and array anymore will return a zero. :TEXT_END: :CODE_START: array set array1 [list {123} {Abigail Aardvark} \ {234} {Bob Baboon} \ {345} {Cathy Coyote} \ {456} {Daniel Dog} ] ;# ;# Simply iterating through an array with a foreach loop: ;# foreach id [array names array1] { puts "$array1($id) has ID: $id" } ;# ;# Two procs iterating through the same array with iteration commands ;# proc getrec_format1 {arrayVar searchid} { global $arrayVar upvar $searchid id set record [array nextelement $arrayVar $id]; return "The current ID is: $record" } proc getrec_format2 {arrayVar searchid} { global $arrayVar upvar $searchid id set record [array nextelement $arrayVar $id]; return $record; } set searchId [array startsearch array1]; puts "" set item 0; while {[array anymore array1 $searchId]} { incr item; if {[expr $item %2]} { set format1 [getrec_format1 array1 searchId] puts "item number: $item format 1: $format1" } else { set format2 [getrec_format2 array1 searchId] puts "item number: $item format 2: $format2" } } :TEXT_END: