Just to elaborate slightly on AnomalousMonk's reply:
A few important sentences from Foreach Loops are:
The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop.
There is a very important point here that is likely to go unnoticed: the for loop variable is an alias to each element of the list in turn. That means that if you change the content of the variable you change the element of the list it is aliased to. It also implies that when the loop exits the variable is no longer aliased to anything - loop variables don't retain their content outside the loop. Consider:
use strict; use warnings; my $var = 3.141; my @array = (1 ..5); for $var (@array) { $var += 10; } print "\$var: $var\n"; print "\@array: @array\n";
Prints:
$var: 3.141 @array: 11 12 13 14 15
In reply to Re: printing a hash
by GrandFather
in thread printing a hash
by mpj196884
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |