After a long time...
This post is about the foreach loop construct. There is something special about the foreach loop, that I noticed recently. Consider this script:
my @array = (1,2,3,4,5,6,7,8,9,0);
foreach my $ele (@array) {
print "$ele\n";
}
Yes, this simply prints the array.
The specialty of foreach is that it assigns the reference of each element to $ele. This is true even if you use an array reference in foreach. This means that if you modify an element inside foreach the array is affected.
my @array = (1,2,3,4,5,6,7,8,9,0);
foreach my $ele (@array) {
print "$ele\n";
$ele = '' if ($ele == 5);
}
Consider above script & then print all the array elements, you will notice 5 is gone.
I have discussed with the perlmonks, visit this thread for more info:
http://perlmonks.org/?node_id=696953
0 comments:
Post a Comment