in reply to "Sparse" Array behavior with "for" and "map"

First off, "we are trying to increment an undef, which is not possible". This is not the case, for example: perl -e '$_ = undef; print ++$_' does in fact print 1. Undef is treated as 0 in a numerical context. Since the for loop hits the undefs, they get incremented to 1.

Similarly, for the map, I have:

$ perl -e '$a = [undef, undef, 3]; map {$_++} @$a; print "@$a"' 1 1 4

Which is the expected result. If you want undef values to be ignored, put an explicit test. I.E. $_++ if defined $_; Also, map in a void context?

Update: Well, you're right, if you create your array your way, it is an attempt to modify a non-existant element. I suggest you just not use the map in a void context. Instead of map {$_++} @a; do @b = map {$_+1} @a; which is safer.