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

I have a simple checking function which i am having trouble with and cannot find the solution. I am basically checking to see if an array has any content. The following code works:
my @modifiedFiles = (); #assuming that this array is empty if (!@modifiedFiles) { exit; }
The follwoing code does not work and gives the error:
my $modifiedFiles = {}; #assuming this array is empty if(!$modifiedFiles->{$streamID}}) { print "EXIT"; exit; }; ERROR: Can't use an undefined value as an ARRAY reference at E:\Inetpu +b\cgi-bin\wms.pl line 163.
THis is quite an obvious error since $modifiedFiles->{$streamID} has no value. Is there however a function i can use to check wether this array exists at all without throwing up an error like this?

costas

Replies are listed 'Best First'.
Re: Simple array checking?
by broquaint (Abbot) on Apr 12, 2002 at 13:28 UTC
    You can check whether a key in a hash exists with the exists() function, and you can check if an array has anything it by comparing it's length to 0. If you want to check whether a given value in a hash is an array you could use ref(), and if so, just dereference it to check it's length.
    my %hash = qw(foo bar baz quux); my $key = "baz"; my @array = qw(x y z); $hash{$key} = \@array; print "$key exists in %hash\n" if exists $hash{$key}; print "\@array has contents\n" if @array > 0; print "\$hash{\$key} is an array\n" if ref($hash{$key}) eq 'ARRAY'; print "\$hash{\$key} is an array with contents\n" if @{$hash{$key}} > 0; __output__ baz exists in %hash @array has contents $hash{$key} is an array $hash{$key} is an array with contents

    HTH

    broquaint

Re: Simple array checking?
by perlplexer (Hermit) on Apr 12, 2002 at 13:26 UTC
    Well, first of all, you have an unmatched curly in there.
    Second, you can't get that error from the code that you posted ($modifiedFiles is a hash ref, not an array ref). What is on line 163 in your script?

    --perlplexer
Re: Simple array checking?
by tachyon (Chancellor) on Apr 12, 2002 at 13:45 UTC

    As perlplexer points out $modifiedFiles is a HASH ref not an ARRAY ref thus the code you post is not the cause of your troubles. You wish to check if a HASH KEY exists, and here is how you do it (you need the defined otherwise the key can exist but have a value of '' or 0 which will be false). unless(blah) is the same as if(! blah)

    my $hash_ref = {}; unless ( defined $hash_ref->{'some_key'}) { print "some_key is not defined\n"; } my $other_hash_ref = { 'some_key' => 0 }; if ( ! defined $other_hash_ref->{'some_key'}) { print "some_key is not defined\n"; } else { print "some_key is defined\n"; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Simple array checking?
by costas (Scribe) on Apr 12, 2002 at 13:39 UTC
    Sorry about the unmatched bracket, mistyped into perlmonks!! line 163=
    if(!(@{$createdFiles->{$streamID}}||@{$modifiedFiles->{$streamID}}||@{ +$deletedFiles->{$streamID}})) { exit; }; Can't use an undefined value as an ARRAY reference at E:\Inetpub\cgi-b +in\wms.pl line 163.
    I have already declared $createdFiles a a hash, the undefined value is i assume the key. I just want to know if they exit or not, i will try the exists function.
      Alright, now we are getting somewhere. :)
      Here's what you can do:
      # !@{$array_ref} <-- Perl will complain about this if $array_ref is un +defined # $#{$array_ref} <-- but not this if($#{$createdFiles->{$streamID}} == -1){ print "Empty\n"; }

      --perlpelxer