Perl each Function
The Perl each function is used to iterate over the elements of an array or a hash. The syntax forms of the each function are as follows:
If you use it with a hash, it returns a two element list consisting of the key-value of the next pair element of the hash.In the case of arrays, this function returns a two element list consisting of the next index and the value of the array element associated with it. Please note that using each with arrays is available starting with the Perl 5.12 version only.
Because the Perl each function doesn't loop by itself, you can wrap it in a while loop to access the elements of a hash or an array. The each function returns false when the end of the hash/array is reached.
The first example shows you how you can use the Perl
each function to print the elements of a hash:
my %hash = qw(1 one 2 two 3 three 4 four);
while (my ($key, $val) = each %hash) {
print("$key=>$val\n");
};
This code produces the following output:
4=>four
1=>one
3=>three
2=>two
The next example shows you how to print the index and the value of the elements of an array, using the Perl
each function and the
while loop:
# starting with Perl 5.12
my @array = qw(one two three four);
while (my ($index, $value) = each @array) {
print("$index, $value\n");
};
The output is as follows:
0, one
1, two
2, three
3, four
Using the Perl
each with the
while loop is very convenient when you need to traverse large arrays or a hashes (get one element at a time). When your aggregates are not so big, you can use the
foreach statement to go through. In the case of hashes, you can iterate through the hash elements by using
foreach and
keys, as you can see in the following snippet:
foreach my $key (keys %hash) {
print("$key => $hash{$key}\n");
};
In a Perl script, each hash or array has its own internal iterator, shared by all of the three functions:
each,
keys and
values. When the
each function will reach the end of the array or hash, this operator will be reset.
Stay assured that the iterator of a hash or array is reset before starting a while loop with each. The internal iterator can be explicitly reset by calling keys or values on a hash/array.
See an example here:
my %hash = qw(1 one 2 two 3 three 4 four);
my ($key, $val) = each %hash;
print "$key => $val\n";
# it prints 4 => four
($key, $val) = each %hash;
print "$key => $val\n";
# it prints 1 => one
# reset the iterator
keys %hash;
while (my ($key, $val) = each %hash) {
print("$key=>$val, ");
};
# it prints 4=>four, 1=>one, 3=>three, 2=>two,
Well, the print examples are demonstrative only, if you run the above examples it's possible to print the hashes elements in a different order, because you couldn't expect at any order when you traverse a hash - this order can be different even between different runs of your script (because of Perl security reasons).
Table of Contents:
A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
Array Size
Array Length
Perl Hashes
Perl Statements
Perl if
Perl unless
Perl switch
Perl while
Perl do-while
Perl until
Perl do-until
Perl for
Perl foreach
Built-in Perl Functions
Functions by Category
String Functions
Regular Expressions and Pattern Matching
List Functions
Array Functions
Hash Functions
Miscellaneous Functions
Functions in alphabetical order
chomp
chop
chr
crypt
defined
delete
each (more)
exists
grep
hex
index
join
keys
lc
lcfirst
length
map
oct
ord
pack
pop
push
q
qq
qw
reverse
rindex
scalar
shift
sort
splice
split
sprintf
substr
tr
uc
ucfirst
undef
unpack
unshift
values
return from Perl each Function to Perl Basics
Would you like to create your own website like this one?
Hit the Alarm Clock!