Foreach may refer to any of the following:

  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”; }

  1. Foreach is also a Linux command, see the foreach command page for further information.
  • How to create a computer program.

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