for Loop
Perl for loop statement to loop over elements of a list.
The Perl for
loop statement allows you to loop over elements of a list. In
each iteration, you can process each element of the list separately. This is
why the for
loop statement is sometimes referred to as foreach
loop.
In Perl, the for
and foreach
loop are interchangeable, therefore, you can
use the foreach
keyword in where you use the for
keyword.
#!/usr/bin/perl
use warnings;
use strict;
my @a = (1..9);
for (@a) {
print("$_","\n");
}
-
First, we defined an array of 9 integers
@a
-
Second, we used
for
loop statement to loop over elements of the@a
array. -
Third, inside the loop, we displayed element’s value using default variable
$_
If you replace the for
keyword by the foreach
keyword in the above example,
it works the same.
#!/usr/bin/perl
use warnings;
use strict;
my @a = (1..9);
foreach (@a) {
print("$_","\n");
}
If we don’t supply an explicit iterator to the loop, Perl will use a special
variable called default variable with the name $_
as the iterator. In each
iteration, Perl assigns each element of the array @a
to the default variable
$_
.
If you want to specify an explicit iterator for the loop, you can declare it in
the for
loop statement as follows:
#!/usr/bin/perl
use warnings;
use strict;
my @a = (1..9);
for my $i (@a) {
print("$i","\n");
}
$i
is the iterator of the for
loop in this example. In each iteration, Perl
assigns the corresponding element of the array to the $i
iterator. Notice
that the $i
variable exists only during the execution of the loop.
If you declare an iterator before entering the loop, Perl will restore its original value after the loop is terminated. Take a look at the following example:
#!/usr/bin/perl
use warnings;
use strict;
my @a = (1..9);
my $i = 20;
for $i (@a) {
print("$i","\n");
}
print('iterator $i is ',"$i","\n"); # 20
-
First, we declared variable
$i
before the loop and initialized its value to20
. -
Second, we used a variable
$i
as the iterator; its value changes in each iteration of the loop. -
Third, after the loop, we displayed the value of
$i
. Perl restored its original value, which is20
.
In each iteration of the loop, Perl creates an alias instead of a value. In other words, if you make any changes to the iterator, the changes also reflect in the elements of the array. See the following example:
#!/usr/bin/perl
use warnings;
use strict;
my @b = (1..5);
print("Before the loop: @b \n");
for (@b) {
$_ = $_ * 2;
}
print("After the loop: @b \n");
-
First, we declared an array
@b
with 5 elements from 1 to 5. We displayed the array@b
elements usingprint
function -
Second, we iterated elements of the array. We multiplied each element with
2
through the iterator$_
-
Third, outside of the loop, we displayed the elements of the array again
Perl also supports for loop in C-style. However, it is not a good practice to use the C-style for loop because to code will become less readable.
for (initialization; test; step) {
// code block;
}
-
Initialization. Perl executes the initialization once when the loop is entered. We often use initialization to initialize a loop counter variable.
-
Test. Perl evaluates the
test
expression at the beginning of each iteration and executes the code block inside the loop body as long as the test expression evaluates to false. -
Step. Perl executes
step
at the end of each iteration. You often use the step to modify the loop counter.
#!/usr/bin/perl
use warnings;
use strict;
my @c = (1..6);
for (my $i = 0; $i <= $#c; $i++) {
print("$c[$i] \n");
}
It is much more readable if you Perl’s for loop style
#!/usr/bin/perl
use warnings;
use strict;
my @c = (1..6);
for (@c) {
print("$_ \n");
}