Monday, December 05, 2005

 

Perl: FOREACH loops

The FOREACH construct takes advantage of perl's builtin list features by
easily iterating over an entire array. The syntax is

foreach SCALAR ( LIST ) {
STATEMENTS;
}

Each time through the loop, SCALAR is assigned the next element in the
LIST, and the STATEMENTS are executed.
The loop flow can be modified using next and last, as in the DO loop.
The braces are mandatory.
Consider this version of join:

foreach $instrument ("violin","viola","cello","bass") {
print($instrument," is lighter than a \n");
}
print("harp\n");


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?