in reply to Is there a better way to check if an array is empty?

So what happened when you tried it?

Did you try it with an empty array?

Did you try it with an array that contains one element?

Did you try it with an array that contains more than one element?

You might also want to try "interesting" values, like 0, the empty string and undef as array values.

Did you check your for loop? Does it execute when there are elements in the array? How often does it execute? Does it skip the body if there are no elements in the array?

What other cases to test for can you think of?

See also perlsyn.

Replies are listed 'Best First'.
Re^2: Is there a better way to check if an array is empty?
by Khariton (Sexton) on Dec 21, 2010 at 11:07 UTC
    If you have one defined element in array then array not empty!
    my $r='undefined'; foreach my $t (@a) { if ($t) {$r='defined';last} } print $r;