This is the Perl tutorial on Perl for loop and Perl foreach from the Perl Tutorials. Perl for loop can be used to run a code block multiple times. Perl foreach loop is a more convenient form of the Perl for loop. First, view the Perl For Foreach tutorial. Then read below.
Perl code can be written in any text editor. The Perl pl script should have the .pl file extension. We can run Perl script within the Command Prompt app. In the Command Prompt
window, change directory using cd command to the folder that has our Perl script. Then run the command, perl scriptname.pl
Using Perl for statement, we can run a code block multiple times depending on an initial value, a condition and an action. Here is the first Perl script example with the Perl for loop. This Perl scripting example should print 1, 2 and 3 on different lines.
use strict; # Perl use strict finds an error in the code and stops the Perl code if there is an error.
use warnings; # Perl use warnings gives a warning in case of an issue with Perl code.
my $i; # In Perl language, Perl my is used to declare a variable in Perl.
# In this Perl for loop, the initial value of variable i will be 1.
use warnings; # Perl use warnings gives a warning in case of an issue with Perl code.
my $i; # In Perl language, Perl my is used to declare a variable in Perl.
# In this Perl for loop, the initial value of variable i will be 1.
# This Perl for loop will check the condition that i is less than or equal to 5. If true, the Perl for loop will be run once.
# This Perl for loop will increase the value of i by 1 i.e. if i was 1, i will become 2 and so on.
for ($i=1;$i<=5;$i++) {
# In Perl scripting language, Perl sleep is used to make the script pause for given seconds.
# if no time is specified, Perl sleep makes the script wait indefinitely.
sleep 1;
print "The counter is $i\n"; # Perl print the variable i
if ($i eq 3){
# Perl break - last to Perl break out of loop
last;
}
}
# In Perl scripting language, Perl sleep is used to make the script pause for given seconds.
# if no time is specified, Perl sleep makes the script wait indefinitely.
sleep 1;
print "The counter is $i\n"; # Perl print the variable i
if ($i eq 3){
# Perl break - last to Perl break out of loop
last;
}
}
Using Perl foreach statement, we can run a code block multiple times for each value in a list of values. Here is the second Perl script example with the Perl foreach loop. This Perl scripting example should print the first value as 1, the final value as 10. Then it should print all values in the list as 1, 2, 3, 5 and 10.
use strict; # In Perl programming, Perl use strict finds an error in the code.
use warnings; # Perl use warnings gives a warning if any issue with Perl code is observed.
# Array variable in Perl can store a list of values.
# Array variable in Perl can store a list of values.
# The Perl array name is prefixed with @.
# An array is declared within parentheses.
my @scores = (1, 2, 3, 5, 10);
# An array element is accessed with square brackets. A Perl array is 0-based.
my @scores = (1, 2, 3, 5, 10);
# An array element is accessed with square brackets. A Perl array is 0-based.
print "The first score is $scores[0] \n";
# One of the Perl special variables, $#array gives the last index of the Perl array.
print "The final score is $scores[$#scores]\n";
foreach (@scores){
# Perl foreach will run for each element of @scores array in Perl
# One of the Perl special variables, $#array gives the last index of the Perl array.
print "The final score is $scores[$#scores]\n";
foreach (@scores){
# Perl foreach will run for each element of @scores array in Perl
# $_ is the default special variable automatically set by the Perl foreach loop.
print "The score is $_\n"; # Perl print array
}
print "The score is $_\n"; # Perl print array
}
Want to learn Perl more or see the above Perl scripts running? Then please view my Perl For Foreach Tutorial. Thank you.