Monday, July 18, 2011

Foreach loop perl

It is a special form of for loop in which is specially used for arrays.
It is used to iterate through each element of the array.


SYNTAX:

foreach LOOP_VAR (ARRAY) {
STATEMENTS
}
In this case for each iteration a element of the ARRAY is stored in LOOP_VAR

Example:

@array=(1..10);
foreach $var(@array)
{
print "The var value is $var"."\n";
}

Output:

G:\perl_programs>perl perl_foreach.pl
The var value is 1
The var value is 2
The var value is 3
The var value is 4
The var value is 5
The var value is 6
The var value is 7
The var value is 8
The var value is 9
The var value is 10

G:\perl_programs>

No comments:

Post a Comment