Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

To test empty array in perl

by siddheshsawant (Sexton)
on Apr 26, 2010 at 14:42 UTC ( #836926=perlquestion: print w/replies, xml ) Need Help??
siddheshsawant has asked for the wisdom of the Perl Monks concerning the following question:


Hello Monks,

good morning !!!

Sorry,My previous version of this question was little misleading so I am re framing it again.

I am working on perl project where I have to test whether the array is empty or not and depending on that I have to an action.The pseudo code for that is like this:

If(all the elements of @array contains  ){ ## Take some action. } else { ## Do something else. }

If my array contains elements like   then I have to take some action.I have used ($array[0] eq " ") in the if condition.Is it a right way to do ? If any body knows about it then kindly let me know about.

Thanks in advance !!!!!

Replies are listed 'Best First'.
Re: To test empty array in perl
by toolic (Bishop) on Apr 26, 2010 at 14:53 UTC
    Evaluating the array in scalar context will let you know if the array has any elements:
    use strict; use warnings; my @array; if (@array) { print "array not empty"; } else { print "array empty"; } __END__ array empty
Re: To test empty array in perl
by Sandy (Curate) on Apr 26, 2010 at 15:36 UTC

    I am a little uncertain as to what you mean when you say that the array is empty, because your pseudo code says

    all the elements of @array contains &nbsp
    This is not the same as an empty array. An empty array has no elements.

    So, if you want to test if any of your array elements have somthing other than blank characters, you would need something different.

    if (! @array) { # there are NO elements, do what you need to do } elsif (grep ! /^\s*$/, @array) { # there are elements in your array, and at least one # of them contains a string that has something other # than spaces } else { # there are elements in your array, but they are either # empty strings, or strings with spaces }
Re: To test empty array in perl
by Ratazong (Monsignor) on Apr 26, 2010 at 14:55 UTC
    What do you mean with array is empty??
    • the @array-variable is not existing(exists)... does only work for single elements ... thanks for the hint moritz!
    • the @array-variable is not defined
    • the array has zero elements (if (scalar(@array) == 0) ...  )
    • each element is undefined my $empty = 1; foreach (@array) { $empty = 0  if (defined ($_)); } if ($empty) ...   )
    • each element is non-existent (similar as above)
    • each element is 0 (similar as above)
    • each element has a "not-defined"_value (similar as above)
    Please select the appropriate solution for your requirements!


    update: See also here: How do I test if an array is empty or not?

      just check. if(defined(@array) { # array defined } else { #not }

        Using defined on an aggregate's been out of fashion for a good while now.

        $ perl -e 'use strict;use warnings;my @foo;if( defined @foo ) { print +"foo" }' |& splain defined(@array) is deprecated at -e line 1 (#1) (D deprecated) defined() is not usually useful on arrays because i +t checks for an undefined scalar value. If you want to see if the array is empty, just use if (@array) { # not empty } for example. (Maybe you should just omit the defined()?)

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: To test empty array in perl
by FunkyMonk (Canon) on Apr 26, 2010 at 16:11 UTC
    Taking "all the elements of @array contains  " literally, you could use code like

    if (grep(/ /, @array) == @array) { say "all elements contain  " }

    Unless I state otherwise, all my code runs with strict and warnings
Re: To test empty array in perl
by nvivek (Vicar) on Apr 27, 2010 at 04:19 UTC

    If you want to check whether the array is empty.You can do in the following ways.

    print "Array isn't empty.Array Values:@array\n" if($array[0]); print "Array isn't empty.Array Values:@array\n" if($#array >= 0); print "Array isn't empty.Array Values:@array\n" if(@array);
      print "Array isn't empty.Array Values:@array\n" if($array[0]);

      This won't tell you whether the array is empty.

      What happens when the first index of array has undef or 0.?

      # my @array = (0, undef, 1, 2, 3); my @array = (undef, undef, 1, 2, 3); print "Array isn't empty.Array Values:@array\n" if($array[0]);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://836926]
Approved by Corion
Front-paged by Corion
[holli]: silent night, perl 6 night
[holli]: is there anybody out there?
[shmem]: the world is full of people - out there...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2017-09-22 20:43 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (269 votes). Check out past polls.