How to Repeat a for Loop Again From Within Loop Perl
Summary: in this tutorial, we will show how to use the Perl for loop argument to loop over elements of a list.
Perl for and foreach statements
The Perl for
loop statement allows you to loop over elements of a listing. In each iteration, you tin procedure 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.
The flowchart of the Perl for
loop is every bit follows:
Perl for loop example
The following example uses the Perl for
loop argument to loop over elements of an array:
#!/usr/bin/perl use warnings; use strict; my @a = (ane..nine); for(@a){ print("$_","\north"); }
Lawmaking linguistic communication: Perl ( perl )
How information technology works.
- Beginning, we defined an array of 9 integers
@a
- 2nd, nosotros used
for
loop statement to loop over elements of the@a
assortment. - Tertiary, inside the loop, we displayed element's value using default variable
$_
. We hash out the default variable$_
in the adjacent section.
If you replace the for
keyword by the foreach
keyword in the above example, it works the aforementioned.
#!/usr/bin/perl use warnings; use strict; my @a = (1..nine); foreach(@a){ print("$_","\north"); }
Code language: Perl ( perl )
for loop iterator
If nosotros don't supply an explicit iterator to the loop, Perl will use a special variable called default variable with the proper noun $_
as the iterator. In each iteration, Perl assigns each element of the assortment @a
to the default variable $_
.
Explicit Perl for loop iterator
If you desire to specify an explicit iterator for the loop, yous can declare it in the for
loop argument as follows:
#!/usr/bin/perl use warnings; employ strict; my @a = (i..nine); for my $i (@a){ impress("$i","\north"); }
Code language: Perl ( perl )
$i
is the iterator of the for
loop in this example. In each iteration, Perl assigns the corresponding chemical 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 earlier entering the loop, Perl will restore its original value after the loop terminated. Take a wait at the following case:
#!/usr/bin/perl employ warnings; utilize strict; my @a = (ane..ix); my $i = xx; for $i (@a){ print("$i","\due north"); } print('iterator $i is ',"$i","\n"); # 20
Code linguistic communication: Perl ( perl )
How information technology works.
- First, we declared variable
$i
earlier 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, afterward the loop nosotros displayed the value of
$i
. Perl restored its original value, which is20
.
Perl for loop iterator: value or alias
In each iteration of the loop, Perl creates an alias instead of a value. In other words, if y'all make any changes to the iterator, the changes also reflect in the elements of the array. See the following instance:
#!/usr/bin/perl employ warnings; use strict; my @b = (1..5); print("Before the loop: @b \n"); for(@b){ $_ = $_ * ii; } print("After the loop: @b \n");
Code linguistic communication: Perl ( perl )
How it works.
- First, we declared an array
@b
with 5 elements from 1 to v. We displayed the array@b
elements usingimpress
function. - Second, we iterated elements of the array. We multiplied each element with
2
through the iterator$_
- 3rd, exterior of the loop, nosotros displayed the elements of the array once again
Perl C-fashion for loop
Perl also supports for loop in C-style. However, it is not a expert practise to apply the C-style for loop because to code will become less readable.
The following syntax illustrates the c-style for
loop in Perl:
for (initialization; test; step) { // code block; }
Code language: Perl ( perl )
There are three control parts:
- Initialization. Perl executes the initialization once when the loop is entered. We oft use initialization to initialize a loop counter variable.
- Test. Perl evaluates the
exam
expression at the beginning of each iteration and executes the lawmaking block within the loop torso every bit long equally the test expression evaluates to fake. - Footstep. Perl executes
step
at the stop of each iteration. You often use the step to modify the loop counter.
The following flowchart illustrates the C-fashion for
loop:
The following example demonstrates how to utilise C-style for
loop:
#!/usr/bin/perl utilize warnings; use strict; my @c = (i..half dozen); for(my $i = 0; $i <= $#c; $i++){ print("$c[$i] \n"); }
Code language: Perl ( perl )
Hence, it is much more than readable if you Perl'south for
loop manner.
#!/usr/bin/perl use warnings; use strict; my @c = (1..six); for (@c){ print("$_ \n"); }
Code language: Perl ( perl )
In this tutorial, y'all've learned about Perl for
loop statement to loop over elements of a list.
Was this tutorial helpful ?
Source: https://www.perltutorial.org/perl-for-loop/
0 Response to "How to Repeat a for Loop Again From Within Loop Perl"
Post a Comment