Foreach

Updated: 11/16/2019 by Computer Hope

Foreach may refer to any of the following:

foreach

1. Foreach is a loop statement in the Perl programming language that executes once for each value of a data object. It takes the general form:

foreach thing (in object) { 
  do something; 
  do something else;
}

For example, the following Perl code block runs once on each element of an array, @values:

Foreach on array in Perl

my @values = ("Computer Hope", "Free computer help", "since 1998.");

foreach $value (@values) {
 print "$value\n";
}

Output

Computer Hope
Free computer help
since 1998.

Foreach on hash in Perl

In the next example, the foreach would step through a hash displaying the hash and the key value.

foreach $example (keys %value) {
 print "$example = $value{$example}\n";
}

2. Foreach is also a Linux command, see the foreach command page for further information.

Do, For, Iteration, Loop, Programming terms, While