1 |
foreach is similar to statement by this name in C-Shell
|
2 |
foreach $index (@some_list returned by an expression perhaps) {
-
statement-block for each value of $index;
-
}
|
3 |
$index is local to this construct and is returned to any value it had before foreach loop executed
|
4 |
An example that also prints 1 to 10 is
|
5 |
@back= (10,9,8,7,6,5,4,3,2,1);
|
6 |
foreach $num (reverse(@back)) {
|
7 |
In above case one can write more cryptically (a pathological addiction of UNIX programmers)
|
8 |
foreach (sort(@back)) { # sort and reverse give same results here
-
print $_,"\n"; # If an expected variable($num here) is omitted
-
}
|