Perl foreach Loop

Perl 循环Perl Loops

The Perl foreach loop is used to iterate over the values of a list or collection variable.

Syntax

The syntax format is as follows:

foreach var (list) {
...
}

Flowchart

Perl 中的 foreach 循环

Example

#!/usr/bin/perl @list = (2, 12, 36, 42, 51); # Execute foreach loop foreach $a (@list){ print "The value of a is:$a\n"; }

After running the above program, the output is:

a 的值为: 2
a 的值为: 12
a 的值为: 36
a 的值为: 42
a 的值为: 51

Perl 循环Perl Loops

Other Extensions