Logo

Programming-Idioms

History of Idiom 7 > diff from v1 to v2

Edit summary for version 2 by :

Version 1

2015-05-06, 21:04:46

Version 2

2015-07-31, 19:16:50

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Code
# For an array
while ( ($idx, $val) = each @array) {
   print "array[$i] = $val\n";
}

# For a hash
while ( ($key, $val) = each %hash) {
   print "hash{$key} = $val\n";
}
Comments bubble
The each iterator returns the index/key and value as a pair for each iteration.