This is part 13 of my series, Basics of ActivePerl. In this part of the series, we look at the foreach-loop, which is an alternative to the for-loop.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
The for-loop Revisited
The following code displays the content of an array using a for-loop:
use strict;
print "Content-Type: text/html\n\n";
my @theArr = ("HBWE", "FGTR", "HTNK", 4587, 4526, 4053, "AB12", "GB58", "TG45", "RE69");
for (my $i = 0; $i < @theArr; ++$i)
{
print $theArr[$i], "
";
}
The first two statements in the code are familiar. The third statement creates the array, with variable, @theArr. Then you have the for-construct. Let us look at what is inside the parentheses of the for-construct first, before we look at what is in the block of the construct.
There are three statements in the parentheses (brackets). The first one is "my $i = 0;" . It gives the starting index value of the iteration. The next statement is, "$i < @theArr;" . This is a statement with two operands: one on the left of < and the other one on the right. Since the one on the left is a scalar, the statement is in scalar context. So @theArr returns a scalar, which is the length of the array. (If you were in the list context, @theArr would have returned the list of elements of the array.) We saw a similar situation previously with the = operator, while here we have the < operator. The third statement in the parentheses increments $i against the next iteration.
Now, inside the block: Inside the block we have one statement. This statement prints the value of each element of the array. It also prints the HTML line break tag. Remember, $theArr[$i] gives the value of each element in the array. For the first iteration, $theArr[$i] would be $theArr[0], for the second, it would be $theArr[1], third, it would be $theArr[2], and so on; note the indices in the square brackets.
The foreach Construct
There is another loop construct, similar to the for-construct. In fact it does the same thing but in a different way. The syntax is:
foreach Vairable (List)
{
#do something
}
You can read the above syntax as, "for each variable in list, do something". Recall, an array is a list. Each array element identifier is a variable. For the above array, $theArr[0], $theArr[1], $theArr[2], etc are all variables. With arrays, the difference in the variable names is in the indices.
Now, read the foreach syntax again. The above syntax code is re-written below using the foreach construct (syntax).
use strict;
print "Content-Type: text/html\n\n";
my @theArr = ("HBWE", "FGTR", "HTNK", 4587, 4526, 4053, "AB12", "GB58", "TG45", "RE69");
foreach my $item (@theArr)
{
print $item, "
";
}
Each variable in the array is represented by the scalar, $item. Note the two places where $item has been used in the construct. The first time it is used in the foreach construct is in the first line; there you have the reserved word, my. The variable can be any scalar name you want. It simply identifies each of the array elements in each iteration. It replaces the would be $theArr[0], $theArr[1], $theArr[2], etc.
List Literal
You can use a list literal in place of the array. The following code illustrates this:
use strict;
print "Content-Type: text/html\n\n";
foreach my $var ('one', 'two', 'three')
{
print $var, "
";
}
No array was created in this code. You have just the list literal, ('one', 'two', 'three'). You should read and try the above three code samples, if you have not done so.
Which one do you use? The for construct or the foreach construct. The Perl inventors prefer that we use the foreach construct.
foreach and hash
The foreach construct can be used with hashes. Here, the variable replaces the keys. Read and try the following code:
use strict;
print "Content-Type: text/html\n\n";
my %fruitColor = (
Apple => "purple",
Banana => "yellow",
Pear => "green",
Lemon => "green"
);
foreach my $var (keys(%fruitColor))
{
print $fruitColor{$var}, "
";
}
The variable that replaces the key is $var. It occurs in the first line and in the block. The list that the foreach construct works on must be in parentheses, that is in (). The function, keys(%fruitColor), produces or returns the list of keys of the hash, %fruitColor. This return list stays in the parentheses. The variable is used in place of the hash keys in the block.
Let us end here. We continue in the next part of the series. Always remember that any principle outlined in this series works with traditional Perl.
Chrys
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- ActivePerl Comparison and Arithmetic OperatorsIn this part of the series, we talk about some common ActivePerl operators.
- ActivePerl Loop StatementsIn ActivePerl you have the do-while loop, the while loop and the for loop. We shall see what all these mean in this article.
- Getting Started with ActivePerlThis is the first part of my tutorial series, Basics of ActivePerl.
- Boolean Logic for ActivePerlIn this part of the series I teach you a way of reasoning called Boolean Logic.
- ActivePerl Conditional StatementsIn this part of the series, we see how a group of statements can be executed based on a condition.
- ActivePerl Variable Types and Context
- ActivePerl Arrays
- ActivePerl Subroutines
- Regular Expressions in Perl for the Novice
- Basics of ActivePerl Variables
- ActivePerl Basic Syntax
- ActivePerl Hashes



