: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.
array names command
array startsearch commands
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.
nextelement. Otherwise, it returns 0.
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.
array names command
array startsearch commands
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.
nextelement. Otherwise, it returns 0.
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.
array names command
array startsearch commands
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.
nextelement. Otherwise, it returns 0.
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: