Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

To test empty array in perl

by siddheshsawant (Sexton)
on Apr 26, 2010 at 14:42 UTC ( [id://836926]=perlquestion: print w/replies, xml ) Need Help??

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

UPDATED:

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
    Hi

    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!

    Rata

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

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: To test empty array in perl
by FunkyMonk (Chancellor) 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found