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

Thanks in advance  :)
-gregpf

###########################################################
# Inspired by Perl Cookbook 1.14, p.30
###########################################################
sub trimLeadingAndTrailingWhitespace()
###########################################################

{
  my @stringsToReturn = @_;

  foreach  my $aString (@stringsToReturn)
  {
    ###  This transfomation operates on the contents of the array.
    ###  That is good for this algorithm, but I would've expected that
    ###  the transformation would occur on a variable local to the foreach{} loop,
    ###  *not* the actual contents of the the array @stringsToReturn
    ###   What is going on ?
    ###  Is  $aString  simply a "pointer" to an array element ?
    ###
    $aString =~ s{^\s+}{};
    $aString =~ s{\s+$}{};  # Use alternative characters like "{}" to delimit patterns
  }




  ## If the caller wants an array, give it,
  ## but if it wants a scalar, just feed back your one and only transformed string
  return( wantarray ? @stringsToReturn : @stringsToReturn[0] );

}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to