CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Question about foreach loop behavior

 



scribby182
New User

Oct 4, 2012, 11:05 AM

Post #1 of 4 (1056 views)
Question about foreach loop behavior Can't Post

First of all, thank you for reading and I apologize for the pretty basic question.

I am writing a simple loop that strips any "/" characters from the end of each entry in an array. I have:

#!/usr/bin/perl

my @postDIRs = ("test1/","test2/");

foreach my $postDIR (@postDIRs) {
print "(A) before = $postDIR\n";
$postDIR =~ s/\/*$//;
print "(A) after = $postDIR\n";
}

foreach (@postDIRs) {
print "(B) after = $_\n";
}

which gives the output:

(A) before = test1/
(A) after = test1
(A) before = test2/
(A) after = test2
(B) after = test1
(B) after = test2

The (A) lines were as I expected, but the (B) lines were surprising. I did not expect that changing $postDIR in the loop would change the original entry in @postDIRs. Is this the usual behavior of a foreach loop? Any explanation about the mechanics of the foreach loop or how this works would be really appreciated.

Thanks for your help,

Scribby182


BillKSmith
Veteran

Oct 4, 2012, 1:25 PM

Post #2 of 4 (1053 views)
Re: [scribby182] Question about foreach loop behavior [In reply to] Can't Post

The short answer is "yes". I agree that it is a bit strange, but it is well documented. Refer to the third paragraph about foreach loops in perldoc perlsyn.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Oct 4, 2012, 3:27 PM

Post #3 of 4 (1048 views)
Re: [scribby182] Question about foreach loop behavior [In reply to] Can't Post

Hi,

when you write:


Code
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:


Code
$postDIR =~ s/\/*$//;


you are actually modifying the original @postDIRs array.

So that when you go through the same array the second time:


Code
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.


scribby182
New User

Oct 5, 2012, 6:52 AM

Post #4 of 4 (1039 views)
Re: [Laurent_R] Question about foreach loop behavior [In reply to] Can't Post

Thank you both for your very clear explanations, this makes complete sense now.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives