Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl foreach Loop

Perl foreach Loop

Perl Loops 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

The foreach Loop in Perl

Example

#!/usr/bin/perl

@list

=

(

2

,

12

,

36

,

42

,

51

)

;

Executing the foreach loop

foreach

$a

(

@list

)

{

print

"

The value of a is:

$a

\n

"

;

}

Running the above program, the output is:



a 的值为: 2

a 的值为: 12

a 的值为: 36

a 的值为: 42

a 的值为: 51

Perl Loops Perl Loops