Updated February 23, 2023
Introduction to Perl foreach
In Perl, foreach is defined as a looping statement which can traverse or iterate through a given list or set of elements one at a time and is the only known for traversing the given set of items when we want to iterate over the items of the set instead of iterating with the entire range given which is done automatically by a foreach loop. In general, we can define foreach for iterating through the set of elements where the variables containing values of these elements of the given set one at a time are sometimes referred to as for loop in Perl and other programming languages.
Working of foreach in Perl with Examples
In this article, we will see foreach loop in Perl, which is mainly used whenever we want to traverse through a given array or list or set of items which means it iterates or traverses the set of values of elements and will further set the variables to each element in the list or set of elements which would be easy for accessing each element one at a time using this foreach loop. In Perl, for and foreach are interchangeable.
In Perl, there is both for and foreach loop used as synonyms whereas for loop uses 3 parts in it separated by a semicolon, but in foreach, we just declare a list or set of elements where there is an option to provide the list as any regular expression also there is no compulsion of specifying the entire list of elements and hence many developers use foreach which makes it easy to write and understand but “for” loop is similar to as in C language so in Perl it is known as C-style “for” loop.
Now let us see syntax and example for demonstrating the foreach loop in Perl:
Syntax:
foreach variable_name (list_of_items) { some executable code }
From the above syntax, we can see that a list is passed where the values of elements in this list are iterated one by one, so the foreach loop in Perl returns each value of the elements in the list and prints the values one by one on the output screen. In this foreach, we need to use “$” for the variable name, which we are specifying between the “foreach” keyword and the list of elements that are specified within the parenthesis. In Perl, another function is provided where it can flatten multiple lists into a single big list that can be specified within the parenthesis separated by a comma with each list names. Note that the list is defined starting with “@” followed by the list name.
Example #1
Code:
print "Demonstration of foreach loop in Perl.";
print "\n";
print "\n";
@lst_1 = ('Educba ', 'Institute ', 'Of ', 'Technology ');
@lst_2 = (1, 3, 5, 7, 9);
print "The list of elements of string type are as follows: ";
print "\n";
foreach $lst_str (@lst_1)
{
print $lst_str;
print "\n";
}
print "\n";
print "The list of elements of numeric type are as follows: ";
print "\n";
foreach $lst_num (@lst_2)
{
print $lst_num;
print "\n";
}
Output:
In the above program, we can see we have declared two lists, one having string and the other having numerical. The above is a simple code for demonstrating the “foreach” loop which; in the first foreach loop, we are using our own variable name such as “lst_str” for referring to each element in the list “@lst_1”, which will print all the string values of the elements in the list. Similarly, the other foreach loop prints the numerical value of elements in the list “@lst_2”.
Now we will see another simple example where we can rewrite the above code; instead of calling foreach twice, we can just specify the list names. This is demonstrated in the below example. So we can say in Perl this foreach loop is very useful when we want to traverse multiple lists at once and print all the values of the elements in all the lists provided.
Example #2
Code:
print "Demonstration of foreach loop in Perl.";
print "\n";
print "\n";
@lst_1 = ('Educba ', 'Institute ', 'Of ', 'Technology ');
@lst_2 = (1, 3, 5, 7, 9);
@lst_3 = ('H', 'E', 'L', 'L', 'O');
print "The list of elements of string type and numeric type in one foreach loop are as follows: ";
print "\n";
print "\n";
foreach $lst_ns (@lst_1, @lst_2, @lst_3)
{
print $lst_ns;
print "\n";
}
print "\n";
Output:
In the above program, we are declaring 3 different lists which have 3 different data types such as string, numeric, and char types. So we can observe we need not use foreach loop for each list to display the elements as we did in the previous example; instead, we can declare only one variable name for displaying all the elements of all the lists that can be specified within the parenthesis as shown in the above code we are using only one foreach loop and the output can be seen in the above screenshot. Therefore this is how the foreach loop helps in writing small codes instead of writing lengthy code.
Now let us see Perl provides for a loop which works the same as the foreach loop; therefore, there is no difference, so they say these are synonyms. But the only difference is the foreach loop processes each instance of each element in a set of elements, whereas for loop can deal with any data not only with collection or set of elements as foreach loop.
Example #3
Code:
print "Demonstration of foreach loop and for loop in Perl.";
print "\n";
print "\n";
print "The use of foreach loop with C-style for loop";
print "\n";
foreach (my $i=0; $i <= 5; $i++) {
print "$i\n";
}
print "The use of for loop with C-style for loop";
print "\n";
for (my $i=0; $i <= 5; $i++) {
print "$i\n";
}
print "The use of foreach loop using regular expression:";
print "\n";
foreach my $x (0..5) {
print "$x\n";
}
print "The use of for loop using regular expression:";
print "\n";
for my $y (0..5) {
print "$y\n";
}
Output:
In the above program, we can see we have written both for and foreach loop for executing the same code, and we can see in the output screen there is no difference, and hence for and foreach loops are synonyms.
Conclusion
In this article, we conclude that foreach in Perl is defined as a loop for traversing the list of elements or set of elements and displaying each value of the elements in the list using a single variable declared in the foreach loop. In this article, we saw a simple example of foreach and saw an example of displaying multiple lists using a single foreach loop. Lastly, we saw for, and foreach loops are synonyms in Perl.
Recommended Articles
This is a guide to Perl foreach. Here we discuss the introduction and working of foreach in Perl with examples, respectively. You may also have a look at the following articles to learn more –