I am trying to write Perl code that checks whether a given portion of a data structure (an array) has elements in it, or not.

It will need to perform this check many many times, so I'm worried about efficiency.

What I'm thinking about writing is

if( @{ $complete->{landmarks}->{LOG}->[$i][$j][$p] } ) { # return early, there's nothing to do here }

The $complete->{landmarks}->{LOG}->[$i][$j][$p] element has to be an array reference (right ?) so I want to test if the array pointed to by that actually holds stuff or not.

What I would hope is that the array is just examined "in place" and it already has some field that records how many elements it contains. Then the check could be performed very quickly. However, for some reason I have the idea that it would copy the whole array to an anonymous location first, and then return the number of elements it copied. Is this even close to the truth?

If so is there a more efficient way to do it?

Thanks in advance,

~tford


In reply to Does @{ } copy arrays? by tford

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.