Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

check array reference empty

by daptal (Acolyte)
on Sep 02, 2011 at 01:52 UTC ( #923743=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I wanted to know what is the best method to check if an array reference exists or not. The following is a basic test i have done.

#!/usr/bin/perl use strict; use warnings; my @arr1 = (1,2,3,4); my @arr2 = (); print ((@arr1) ? "arr1 is defined\n" : "arr1 is not defined\n"); print ((@arr2) ? "arr2 is defined\n" : "arr2 is not defined\n"); my $arr_ref1 = [11,22,33,44]; my $arr_ref2 = []; print (($arr_ref1) ? "arr_ref1 is defined\n" : "arr_ref1 is not define +d\n"); print (($arr_ref2) ? "arr_ref2 is defined\n" : "arr_ref2 is not define +d\n"); print (($arr_ref1->[0]) ? "arr_ref1 is defined\n" : "arr_ref1 is not d +efined\n"); print (($arr_ref2->[0]) ? "arr_ref2 is defined\n" : "arr_ref2 is not d +efined\n");
Output:
arr1 is defined arr2 is not defined arr_ref1 is defined arr_ref2 is defined arr_ref1 is defined arr_ref2 is not defined

The above method ie $arr_ref->[0] will fail in conditions when the first element is set to undef and the rest of the elements exist.
Your thoughts please

Replies are listed 'Best First'.
Re: check array reference empty
by jwkrahn (Monsignor) on Sep 02, 2011 at 02:06 UTC

    Although your message says "defined" you need to use the defined function to test whether a scalar is defined.

    To determine if a scalar contains a reference to an array use the ref function: ref( $arr_ref1 ) eq 'ARRAY'.

    To determine if $arr_ref1->[0] exists then test the size of the array @$arr_ref1 >= 1

Re: check array reference empty
by ikegami (Pope) on Sep 02, 2011 at 03:31 UTC

    It's unclear to me what you want, but then again, I can't seen to focus right now.

    If $arr_ref is an array reference, and you want to check if there are any elements in the referenced array, the following will do:

    if (@$arr_ref)

    If $arr_ref can be either undef or an array reference, and you want to check if there are any elements in the referenced array (if any), the following will do:

    if ($arr_ref && @$arr_ref)

    If $arr_ref can be either undef or an array reference, and you want to check if it's an array reference, the following will do:

    if ($arr_ref)
Re: check array reference empty
by Marshall (Abbot) on Sep 03, 2011 at 01:12 UTC
    Any element of any array can be "undefined".
    A reference to an array is defined if the array exists. That array may contain Zero (No) elements.
    #!/usr/bin/perl -w use strict; my $arr_ref2 = []; print "".defined $arr_ref2 ? "arr_ref2 is defined\n" : "arr_ref2 is not defined\n"; print "".defined $arr_ref2->[0] ? "arr_ref2->[0] is defined\n" : "arr_ref2->[0] is not defined\n"; my $arr_ref3 = [1,2,undef,4]; # [2] is undefined! print "\n"; print "".defined $arr_ref3->[2] ? "arr_ref3->[2] is defined\n" : "arr_ref3->[2] is not defined\n"; print "".defined $arr_ref3->[1] ? "arr_ref3->[1] is defined\n" : "arr_ref3->[1] is not defined\n"; __END__ arr_ref2 is defined arr_ref2->[0] is not defined arr_ref3->[2] is not defined arr_ref3->[1] is defined
Re: check array reference empty (Please Ignore)
by luis.roca (Deacon) on Sep 02, 2011 at 02:11 UTC

    Please Ignore: I realized after adding a note that this skips empty values, essentially all but answering the OP's question.

    One easy way would be to use Data::Dumper

    #!/usr/bin/perl use strict; use warnings; use diagnostics; use Data::Dumper; my @arr1 = (1,2,3,4); my @arr2 = (); my $arr_ref1 = [11,22,33,44]; my $arr_ref2 = []; # The last scalar ($VAR8 when printed) does not exist so will return ' +undef' print Dumper( @arr1, @arr2, $arr_ref1, $arr_ref2, $$arr_ref1[0], $$arr_ref2[1] ); __END__ # RETURNS $VAR1 = 1; $VAR2 = 2; $VAR3 = 3; $VAR4 = 4; $VAR5 = [ 11, 22, 33, 44 ]; $VAR6 = []; $VAR7 = 11; $VAR8 = undef;

    Note that @arr2 has been skipped but $arr_ref2 has not. Remember that while the first has not been assigned a value the second has: It just so happens to be an empty array.


    "...the adversities born of well-placed thoughts should be considered mercies rather than misfortunes." Don Quixote

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2019-10-20 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?