jockel has asked for the wisdom of the Perl Monks concerning the following question:
Hi
Either I'm missing something, or I've found a bug..
The following code should in my opinion print out the same in the first and second loop. But somehow the middle loop actually alters the array.
What is happening?
#!/usr/bin/perl my $arrayref = ['A','_B','C']; foreach my $str (@{$arrayref}) { print "BEFORE: str = $str\n"; } foreach my $str (@{$arrayref}) { $str =~ s/^\_//g; } foreach my $str (@{$arrayref}) { print "AFTER: str = $str\n"; }
OUTPUT:
BEFORE: str = A BEFORE: str = _B BEFORE: str = C AFTER: str = A AFTER: str = B AFTER: str = C
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Possible bug with array pointer
by haukex (Archbishop) on Jan 10, 2017 at 16:32 UTC | |
by jockel (Beadle) on Jan 10, 2017 at 18:59 UTC | |
by Athanasius (Archbishop) on Jan 11, 2017 at 03:34 UTC | |
by jockel (Beadle) on Jan 11, 2017 at 14:36 UTC | |
by Marshall (Canon) on Jan 11, 2017 at 01:40 UTC | |
by jockel (Beadle) on Jan 11, 2017 at 14:43 UTC | |
by haukex (Archbishop) on Jan 11, 2017 at 14:57 UTC | |
|
Re: Possible bug with array pointer
by SuicideJunkie (Vicar) on Jan 10, 2017 at 16:30 UTC |