Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Check if array is null

by Texan (Acolyte)
on Oct 06, 2004 at 14:29 UTC ( [id://396995]=perlquestion: print w/replies, xml ) Need Help??

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

Is there any way to check if an array has been initialized? I am executing a command and that output is going into an array. If the command has no output then the array does not get initialized. Is there a way to check that?
Thanks

Replies are listed 'Best First'.
Re: Check if array is null
by Limbic~Region (Chancellor) on Oct 06, 2004 at 14:34 UTC
    Texan,
    • Has been initialized/defined - if ( defined @array ) {...}
    • Has at least 1 element - if ( @array ) {...}
    • Has at least 1 element defined - if ( grep defined , @array ) {...}

    Cheers - L~R

      perldoc -f defined
      Use of "defined" on aggregates (hashes and arrays) is
      deprecated. It used to report whether memory for that aggregate
      has ever been allocated. This behavior may disappear in future
      versions of Perl. You should instead use a simple test for size:
      
          if (@an_array) { print "has array elements\n" }
          if (%a_hash)   { print "has hash members\n"   }
        Anonymous Monk,
        Thanks for another example of the importance of learning to fish - RTFMing. It also points out that no matter how good of a fisherman we think we are - there is always more to learn.

        Cheers - L~R

      Your first and second are identical in function:

      our @array2; my @array3; my @array4 = (); my @array5 = (undef); my @array6 = ('one'); print("defined:\n"); print('array1: ', defined(@array1)?1:0, "\n"); # 0 print('array2: ', defined(@array2)?1:0, "\n"); # 0 print('array3: ', defined(@array3)?1:0, "\n"); # 0 print('array4: ', defined(@array4)?1:0, "\n"); # 0 print('array5: ', defined(@array5)?1:0, "\n"); # 1 print('array6: ', defined(@array6)?1:0, "\n"); # 1 print("\n"); print("explicit scalar:\n"); print('array1: ', scalar(@array1)?1:0, "\n"); # 0 print('array2: ', scalar(@array2)?1:0, "\n"); # 0 print('array3: ', scalar(@array3)?1:0, "\n"); # 0 print('array4: ', scalar(@array4)?1:0, "\n"); # 0 print('array5: ', scalar(@array5)?1:0, "\n"); # 1 print('array6: ', scalar(@array6)?1:0, "\n"); # 1 print("\n"); print("implicit scalar:\n"); print('array1: ', @array1?1:0, "\n"); # 0 print('array2: ', @array2?1:0, "\n"); # 0 print('array3: ', @array3?1:0, "\n"); # 0 print('array4: ', @array4?1:0, "\n"); # 0 print('array5: ', @array5?1:0, "\n"); # 1 print('array6: ', @array6?1:0, "\n"); # 1 print("\n");

      I don't think defined should be used on an array.

        ikegami,
        Your first and second are identical in function

        Not according to my test:

        #!/usr/bin/perl use strict; use warnings; my @array = (undef, undef); print "foo\n" if defined @array; print "bar\n" if grep defined , @array; __END__ $ perl foo.pl defined(@array) is deprecated at foo.pl line 5. (Maybe you should just omit the defined()?) foo $ perl -v This is perl, v5.8.5 built for cygwin-thread-multi-64int

        Cheers - L~R

Re: Check if array is null
by Joost (Canon) on Oct 06, 2004 at 16:06 UTC
    Is there any way to check if an array has been initialized? I am executing a command and that output is going into an array. If the command has no output then the array does not get initialized. Is there a way to check that?
    Your assumptions are wrong:

    after

    my @array1; my @array2 = some_code();
    is run, @array1 and @array2 will both be initialized. @array1 will be empty: you can check for that using:
    if (@array1) { print "not empty\n"; }

    @array2 will contain whatever some_code() returns. some_code() will allways return a list, because it's called in list context (caused by the assignment to @array2).

    If some_code returns like this:

    return (); # an empty list
    @array2 will be empty, like @array1 above.

    If some_code returns like this:

    return undef; # undefined scalar
    @array2 will contain 1 element which will be undefined.

    If some_code returns like this:

    return; # "return false"

    @array2 will be empty (return; without arguments returns an empty list in list context, and undef in scalar context)

    If some_code does not use a return statement, the last statement in some_code will be executed in list context and its value will be returned.

Re: Check if array is null
by duff (Parson) on Oct 06, 2004 at 14:47 UTC

    You probably want the middle of Limbic's answers above.

Re: Check if array is null
by ikegami (Patriarch) on Oct 06, 2004 at 17:57 UTC

    For fun, here's something that works with non-lexical arrays:

    sub get_ref { my ($type, $varname) = @_; return do { no strict 'refs'; *$varname{$type} }; } @array2; @array3 = (); @array4 = (undef); @array5 = ('one'); @bla::array6; $\=$/; print('array1: ', defined(get_ref(ARRAY => 'array1 '))?1:0); # 0 print('array2: ', defined(get_ref(ARRAY => 'array2' ))?1:0); # 1 print('array3: ', defined(get_ref(ARRAY => 'array3' ))?1:0); # 1 print('array4: ', defined(get_ref(ARRAY => 'array4' ))?1:0); # 1 print('array5: ', defined(get_ref(ARRAY => 'array5' ))?1:0); # 1 print('array6: ', defined(get_ref(ARRAY => 'bla::array6'))?1:0); # 1

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-18 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found