Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do I test if an array is empty or not?

by Anonymous Monk
on Nov 12, 2000 at 15:19 UTC ( [id://41177]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

How do I test if an array is empty or not?

Originally posted as a Categorized Question.

  • Comment on How do I test if an array is empty or not?

Replies are listed 'Best First'.
Re: How do I test if an array is empty or not?
by merlyn (Sage) on Nov 12, 2000 at 16:42 UTC
    The name of the array (like @a) in a scalar context yields the number of elements currently in the array. And a boolean test selects scalar context automatically, so something like:
    if (@a) { # @a is not empty... ... } else { # @a is empty ... }
    should work just fine.
Re: How do I test if an array is empty or not?
by Fastolfe (Vicar) on Nov 12, 2000 at 21:57 UTC
    As a supplement to merlyn's answer, never be tempted to use defined on an array (or a hash for that matter). Simply testing the value of the @array in a scalar context is sufficient. The defined function makes little sense on any data type other than a $scalar.
Re: How do I test if an array is empty or not?
by quidity (Pilgrim) on Nov 12, 2000 at 22:43 UTC

    The answer here does depend a little bit on what you mean by 'empty'. The earlier answers only tell you if an array has any elements, but not tell you what they contain.

    If by empty you mean 'only contains defined elements' then you will need the following test instead:

    my @a = (0,1,2,3); foreach (@a) {print "'$_'\n";} # Prints each element. print "\@a exists\n" if @a; print "\@a is not Empty\n" if grep {defined($_)} @a; # Prints '@a is not empty' if any element in '@a' is defined. my @b = (1, undef) foreach (@b) {print "'$_'\n";} print "\@b exists\n" if @b; print "@b is not Empty\n" if grep {defined($_)} @b;

    Of course, if you want to know if the array contains only true values, then you can get away without using the defined() bit.

    Edited: Examples reworked by davido.
Re: How do I test if an array is empty or not?
by rajib (Novice) on Aug 20, 2002 at 18:26 UTC
    my $j = 0; for (my $i = 0; $i <= $#array; ++$i) { $current = $array[$i]; if($current == undef) { push @empty, $j++; } } print "undefined indices: @empty \n";
    or to put it in common perl terms
    my @empties = (); for my $i(0..$#array) { unless(defined $array[$i]) { push @empties, $i; } } print "undefined indices @empties\n";

    {Editor's note: This example doesn't work as the poster thought it would. See Abigail-II's explanation here: Re: Answer: How do I test if an array is empty or not?.}

    Edited by davido for clarification.
      What your code is trying to do is extracting the undefined values (or rather, the indices) from the array - which is a different question. However, your first code fragment not only finds the indices with undefined values, but anything that is 0 in numeric context, which includes 0, the empty string and the undefined value.

      Your second code fragment is correct, but it can be written in a more Perlesque way:

      my @empties = grep {!defined $array [$i]} 0 .. $#array;

      As for the question, just use the array in scalar context. It's empty if the array is 0 in scalar context.

      Abigail

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://41177]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-07-23 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.