Perl foreach Loop Statement

Perl foreach loop Menu:

1. The syntax forms
    1.1. General foreach statement
    1.2. foreach used as a modifier (a short form)


Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!


The foreach statement is specially designed to iterate over lists or arrays: it steps through each element of a list or an array, using an iterator.

This short free tutorial will step a bit through some important features of this function and it will give you a few examples to see how to use it in a Perl script.

The foreach statement is a control looping statement and is very useful whenever you need to perform some actions on the individual elements of a list/array.

Before going on, I want to make a short note about lists and arrays here. Although they seem to represent the same thing, they don’t have the same meaning. Take a look at the next line of code:

@arrayName = (1, 2, 3, $v1);

On the left side we have an array which we can refer throughout the Perl script by its name: arrayName. On the right side of the = sign we have a list. The above code assigns the list to the array. The foreach statement is meant to work as argument with either a list or an array.

I want to mention that the C-style for and the foreach statements are not the same thing. For readable purposes, you can spell foreach as for and the Perl interpreter will know to distinguish between them. If there are two semicolons (;) that separates the arguments of the for statement you have a C-style for, otherwise a foreach statement spelled as for. The main difference between for and foreach is that for can be used for more complex operations on the iterators, as long as foreach provides a way to loop through the elements of a list without having to provide the number of elements of the list.

Next we will use foreach name throughout our examples in order to avoid any ambiguity.

1. The syntax forms


The Perl foreach loop statement has a general syntax form and a short one where it is used as a modifier of a statement.

    1.1. General foreach statement


The most general syntax form for the foreach statement is as follows:

LABEL foreach VAR (LIST) BLOCK continue BLOCK

where:

  • LABEL is an identifier, usually in uppercase followed by a semicolon and it is optional; you can use it as an anchor for jumping to from within the block just in case you need to use any of the three looping controls (next, last or redo – not goto please, I don’t think that is quite a wise option) to alter the block flow control.
  • VAR is an iterator variable used to loop over all the elements of a list - at each iteration step, this variable is set to each successive element of the list; it is optional, therefore could be omitted – in this case the special variable $_ is used instead. If you’ll use the Perl foreach statement with $_, please note that the block may contain other statements that use this variable too and there are chances to alter its value. If you declare VAR with my, the scope of the variable will extend throughout the foreach statement, but not beyond it.
  • LIST is a list of scalars or an array that points to a list; it is the argument of the Perl foreach loop statement and must be enclosed in round parentheses ().
  • BLOCK – is a group of one ore more statements enclosed in curly braces {}. The first block that follows the list argument is mandatory and it will be executed for each element of the list, meanwhile the block that follows the continue clause is optional and is only present if the continue clause is used.
  • continue – is optional, but if present, the block that is after it will be executed after each iteration. It’s useful whenever you have a block of statements to execute after the current iteration. On the other hand, it is not very often used.

How to use Perl foreach with a list

my $min = 999;
foreach $item (1, 2, 3, 4, 5)
{
    $min = $item if $min > $item;
}
print "Min = $min\n";      # expects Min = 1

In the above sample code I used the foreach loop to iterate over a list of five numbers in order to print the minimum value. At each step of iteration we compare $min variable with the current element of the list and if the current element of the list is less than $min, we assign the new value to $min.

After finishing the loop, we’ll get the minimum value of the numbers contained in the list. During the execution of the Perl foreach statement the $item variable will be successively set to each element of the list. Please note that when we use the foreach statement, we don’t need to know how many elements has the list, Perl will take care of this.

How to use Perl foreach with an array

We’ll rewrite the code above in order to print the maximum number from the list, but this time we’ll use an array:

my $max = -999;
# initialize an array
@numbers = (1, 2, 3, 4, 5);
foreach $item (@numbers)
{
    $max = $item if $max < $item;
}
print "Max = $max\n";      # expects Max = 5

How to use Perl foreach with the special variable $_

The next example will show you how to print the sum of the numbers from the list, using the special variable $_.

my $sum = 0;
@numbers = (1, 2, 3, 4, 5);

foreach (@numbers)
{
    $sum += $_;
}
print "Sum = $sum\n";      # expects Sum = 15

The elements of the array will be successively stored in the special variable $_ which will be used inside the block.

How to use Perl foreach with STDIN

If we use standard input operator in a list context, Perl interpreter will read first all the lines from STDIN as elements of a list and only afterwords the foreach statement will iterate over this list:

# initialize an array
@colors = ();
foreach (<STDIN>)
{
  chomp;  
  # append the line to the array
  push colors, $_; 
}   
print "@colors\n";

a possible output after running this code could be:

blue
white
^Z
blue white

Each line from the list is read in the special variable $_. We used the chomp function against $_ to chomp off the trailing newline and the push function to append the line to the array. Please note that when we called the push function we didn’t need to use the @ symbol to precede the name of the array, because Perl expects an array there.


How to use Perl foreach with the range operator ..

As you know, in a list context the range operator will return a list with contiguous sequences of items, beginning with the left operand value and ending with the right operand value. See a simple example about how to use it with Perl foreach loop statement:

$sum = 0;
foreach my $i (15..20) {
     $sum += $i;
}
print "Sum = $sum\n";    # expects Sum = 105

If you run this code, you will get the amount of integer numbers between 15 and 20.

How to use Perl foreach with a hash

Let’s examine now a simple snippet code that illustrates a common use of Perl foreach loop statement in connection with hashes (associative arrays).

# initialize a hash structure (name, age)
%persons = (John => 25, Anne =>32, Paul =>22, Smith => 29);
foreach $name (sort keys %persons) {
  print "$name is $persons{$name} years old.\n";
}

After running this code, you’ll get the output:

Want more examples?
Try this resource!
Anne is 32 years old.
John is 25 years old.
Paul is 22 years old.
Smith is 29 years old.

This example uses foreach, keys and sort to iterate over the %persons array and print the person names in alphabetical order. By using keys, Perl will create a list containing all the keys of the hash structure, will sort the keys in alphabetical order and only afterwards will begin looping over the list of keys. Because of this you must have in view two things:

  • first, this code is OK to use for hashes that do not contain a large number of elements; if your hash is huge, using the while loop is more appropriate – see the next example for this
  • second, in the Perl foreach block do not add or delete keys because the list of keys over which foreach will loop is created before the beginning of the Perl foreach loop statement; if you do so, the keys will be added or deleted from the hash table, but the foreach statement will not work properly.
If you have a big hash, then you probably want to work across the hash elements by reading one element at a time, and not to store all the keys in memory at once. For this case, using the while statement is more appropriate:

# initialize a hash structure (name, age)
%persons = (John => 25, Anne =>32, Paul =>22, Smith => 29);
while (($name, $age) = each (%persons)) {
  print "$name is $age years old.\n";
}

The each function will get a pair (key, element) one at a time and so it will not use a big chunk of memory to store the pairs. A similar remark as before, if you use each to get the pairs, do not add or delete keys within the while block.

This code will print the keys in the internal order of the hash table, and this order is neither alphabetic nor numeric. The while loop will end when each function will return an empty list () (no more pair elements to get).


How to use nested foreach

The following example will create the @numbers array by multiplying each element of the list (1..3) with each element of the list (1..5) and storing the result in the @numbers array (the push function). Finally it will print the elements of the array (5 elements on a line) using the C-type for statement.

@numbers = ();
foreach $i (1..3) {
  foreach $j (1..5) {
     push @numbers, $i * $j;
  }
}
for($k = 0; $k < 25; $k++) {
  print "\n" if $k%5 == 0;
  print $numbers[$k], " ";
}

The output is as follows:

1 2 3 4 5
2 4 6 8 10
3 6 9 12 15

How to use Perl foreach with continue, last, next, redo

use warnings;

foreach $i(1..5) {
  chomp(my $line = <STDIN>);
  last if $line =~ /quit/;
  next if $line =~ /for|foreach/i;
  redo;
}
continue {
  $count = $i;
}
# end of the loop
print "Count = ", $count, "\n";

An example of output:

for
foreach
for
foreach
www
www
www
for
Count = 5

The example above will print the number of times we typed the word 'for' or 'foreach'. The loop will end either if we typed five times the word 'for' or 'foreach', or if we typed the word 'quit'.

Please note that the redo control will reiterate the loop by skipping the continue block and without modifying the $i looping variable.

The next control will skip the statements that are after it but it will go on with the continue block.

The last control will finish the loop when we typed the word 'quit', and will execute the continue block no more. See the next example for this:

foreach (qw(a b c d)) {
  last if $_ eq 'c';
}
continue {
  print $_, "\n";
}

The output is as follows:

a
b

foreach will loop through the elements of the list, storing each element in the special variable $_ successively. When it will read the third element that is equal with 'c', Perl will end the loop without executing the continue block.

How to use Perl foreach with @_

To see this topic you need to click here to watch this video on youtube.

    1.2. foreach used as a modifier (a short form)


STATEMENT foreach (LIST)

This second form uses Perl foreach loop statement as a modifier of a statement (this is a short syntax form for the foreach statement). You can see an example next:

# initialize an array
@sentence = ("This ", "is ", "a ", "line ", "of ", "text ", "\n");
print foreach @sentence;
# it prints: This is a line of text

Each element in the list will be assigned to the special variable $_ and printed one at a time.

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!


Check these how-to tutorial eBooks (PDF format):


Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach (more)
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        delete
        each
        exists
        grep
        hex
        index
        join
        keys
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        q
        qq
        qw
        reverse
        rindex
        scalar
        shift
        sort
        splice
        split
        sprintf
        substr
        tr
        uc
        ucfirst
        undef
        unpack
        unshift
        values

return from Perl foreach to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!