> -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, August 21, 2001 1:30 PM > To: [EMAIL PROTECTED] > Subject: foreach() loop creates $aString-s that I modify , AND they > modify the Array Contents !? > > > Hi Folks, > > In the subroutine below, > I am using a "foreach" loop to loop loop through a scalar > variable named > $aString . > > I change the value of $aString but ... it turns out that I am **also** > altering the values of the Strings held in the Array. > > Why does this happen ? (See code below). >From perldoc perlsyn: Foreach Loops ... If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that element will fail. In other words, the "foreach" loop index variable is an implicit alias for each item in the list that you're looping over. If you don't want this behavior, make a copy of the list element inside the loop: for (@array) { my $copy = $_; # now changes to $copy will not affect @array element } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]