
Laurent_R
Enthusiast
Oct 4, 2012, 3:27 PM
Post #3 of 4
(654 views)
|
|
Re: [scribby182] Question about foreach loop behavior
[In reply to]
|
Can't Post
|
|
Hi, when you write:
foreach my $postDIR (@postDIRs) { print "(A) before = $postDIR\n"; $postDIR =~ s/\/*$//; print "(A) after = $postDIR\n"; } you have to understand that $postDIR is not a copy of the individual values of the elements of the @postDIRs array, but an alias for each element (for performance reasons, the values are not copied to a new variable, $postDIR is each time through the loop an alias to the real value of the array). In other words, when you modify $postDir in the following line: you are actually modifying the original @postDIRs array. So that when you go through the same array the second time:
foreach (@postDIRs) { print "(B) after = $_\n"; } the array has been modified by the first foreach loop, and the "\" are already gone, as the elements were modifies in the first loop.
|