Update: If you didn't catch it, I was being sarcastic on the "wasted" space. It's itsy-bitsy by perl standards.

Exactly! I was missing the forest for the trees. Take a close look at the memory addresses being used here. I swapped the undef elements for numeric 1 just to see a real SvIV. I think what actually happens here is that the ARRAY pointer array is fully allocated out to the limit of @ary. The unused elements appear to be null pointers or at least all share a single empty Sv struct with no allocated svpv structures. So actually... that array with 1001 elements appears to have consumed enough memory for five elements plus an additional 994 32 bit pointers for a whopping 3K of "wasted" space. If I'm wrong on what this actually looks like then that'd be really good to know.

My interpretation is that arrays are sparse by default when there are uninitialized elements. Getting a sparse array to work nicely under iteration operators appears to require some additional footwork. This looks like the sort of thing you'd want to restrict to knowledgable perl users since it doesn't quite work with the normal perl idioms and is highly likely to confuse (but is a useful thing to have). If anyone knows of a different way to go directly from real element to real element that'd really keen.

@ary[1,100,500,1000] = (1) x 4; # Iterate a sparse array exists($ary[$_]) and print "$_\n" for 0 .. $#ary; # Iterate a normal array print "$_\n" for @ary
Elt No. 994 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 995 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 996 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 997 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 998 0x4284 SV = NULL(0x0) at 0x4284 REFCNT = 2147483634 FLAGS = (READONLY) Elt No. 999 0x706c SV = IV(0x12094) at 0x706c REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1

Fun Fun Fun in the Fluffy Chair


In reply to Re^2: Sparse arrays? by diotalevi
in thread Sparse arrays? by diotalevi

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.