Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
use 5.016; my @array = 0..5; my @incremented = map { $_++ } @array; say "@array"; # 1 2 3 4 5 6 say "@incremented"; # 0 1 2 3 4 5 my @array = 0..5; my @doubled = map {$_ * 2} @array; say "@array"; # 0 1 2 3 4 5 say "@doubled"; # 0 2 4 6 8 10
Why is @incremented not incremented but @doubled is doubled? And why is @array affected in the first case by manipulating $_ but not the second? I'm reading perldoc -f map to see a reason but I'm not grokking it so far. I know to use foreach to modify the org array if I had to - just wondering why map is acting like this. Thanks for any insight.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: map confusion
by GrandFather (Saint) on Dec 15, 2015 at 23:48 UTC | |
by Anonymous Monk on Dec 15, 2015 at 23:53 UTC | |
by FreeBeerReekingMonk (Deacon) on Dec 16, 2015 at 00:04 UTC | |
|
Re: map confusion
by Cristoforo (Curate) on Dec 16, 2015 at 00:12 UTC | |
by Anonymous Monk on Dec 16, 2015 at 00:43 UTC |