Latest web development tutorials

Perl foreach loop

Perl cycle Perl cycle

Perl foreach loop to iterate over a list or a set of values ​​of the variables.

grammar

The syntax is as follows:

foreach var (list) {
...
}

flow chart

In Perl foreach loop

Examples

#!/usr/bin/perl

@list = (2, 12, 36, 42, 51);

# 执行foreach 循环
foreach $a (@list){
    print "a 的值为: $a\n";
}

The above program, the output is:

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

Perl cycle Perl cycle