Indeed. I stumbled over the Further subtleties in another recent thread, trying to explain what was happening if an array with holes is passed into a sub. ysth corrected me there.
There's a distinction between nonexistent and nonexistent; if you pass array elements into to a sub, even nonexistent slots will be created and assigned, and the underlying array expanded as appropriate, whereas if you pass in an array, it is flattened into a list, and nonexisting array slots aren't made into aliases.
The behaviour might be called a bug or a malfunction, but thinking about it - it could be taken as a feature also, e.g. for chaining subs where you pass @_ along, as in
#!/usr/bin/perl $\ = "\n"; @y[3,5] = (13,15); @x = x(@y); print '@y = (',join (',', map {"'$_'"} @y),')'; print '@x = (',join (',', map {"'$_'"} @x),')'; sub x { $_++ for @_; @_[1,4] = (2,5); &z; } sub z { $_ *= 3 for @_; @_; } __END__ @y = ('','','','42','','48') @x = ('3','6','3','42','15','48')
and expect non-existing slots of the original array not to be modified, i.e. you want the holes preserved. I guess that could make perfect sense for e.g. matrix operations, where you don't want undefined elements of sparse arrays spring into existense merely because you $_ **= 2 for @_.
Of course that behaviour should be documented somewhere (not only in PM threads :-)
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
In reply to Re: map vs for (not the usual breed)
by shmem
in thread map vs for (not the usual breed)
by blazar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |