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.
In reply to map confusion by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |