I just noticed this thread, so forgive me if someone has already mentioned
this, or if I'm missing the original point.  I just saw some bad examples of
how to accomplish what the subject asks, and felt I should chime in.

The idiomatic method for checking if an array has elements is simply:

    if (@array) { ... }

If you've stored an array reference, as in $ref = \@array, then dereference:

    if (@$ref) { ... }

See perldoc perldata for help on dealing with data types, perldoc perlref
and perldoc perldsc for help on dealing with references.


Checking an individual element to see if an array is empty will not work. 
One of the examples was:

    if (!defined $ref->[0]) { ... }

Given:

    @array = (undef, "foo", "bar");
    $ref = \@array;

That test will return a false positive; defined($ref->[0]) will be false,
yet there are elements in @array, and by extension, @$ref.


Another of the examples was:

    if (!$array[0]) { ... }

Given:

    @array = (0, "foo", "bar");

That test will also return a false positive; $array[0] is false, yet there
are elements in @array.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to