Using each() to iterate over an array

In perl 5.12, each() - as well as keys() and values() - works on arrays, where previously these functions were restricted to hashes. When iterating over an array, each() returns both the index and the value.

my @x = 50..59;
while(my ($i, $v) = each @x) {
    say "index $i, value $v";
}

will print:

index 0, value 50
index 1, value 51
index 2, value 52
index 3, value 53
index 4, value 54
index 5, value 55
index 6, value 56
index 7, value 57
index 8, value 58
index 9, value 59

Previously you could iterate over the array values but had to keep a counter yourself, or you could have a for-loop counting up, but then you had to retrieve the current array element separately. So this new construct saves quite a bit of code.

On a related note, if you need to iterate over an array, taking several elements at a time, you can use the well-known idiom:

while (my ($f, $g) = splice @x, 0, 2) {
    say "f $f, g $g";
}

which will print:

f 50, g 51
f 52, g 53
f 54, g 55
f 56, g 57
f 58, g 59

2 Comments

I never use values() and rarely use each() I think using keys() or iterating through array index/element is more clear when you read the code.

Leave a comment

About Marcel Grünauer

user-pic perlservices.at is a B2B service provider for Perl in Vienna, Austria. I'm a professional software developer since 1989 and a Perl specialist since 1998. I also like Go (囲碁), Japanese and Korean culture and language.