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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.