http://www.perlmonks.org?node_id=801632

tford has asked for the wisdom of the Perl Monks concerning the following question:

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