in reply to One liner to Check and undef hash elements

Is there a quick, perlish way to perform either of these 2 operations

What about a way to do both :-)

if(scalar @{[ delete @hash{keys %hash} ]}) { print "Hash was not empty\n"; } else { print "Hash was empty\n"; }

The delete function returns a list of the deleted values. If there were no keys in the hash then the list will be empty. I found that if the hash contained one key with a value of undef then scalar delete @hash{keys %hash} evaluated to false - hence the anonmous array gymnastics.

My code is actually checking whether the hash was empty (and in the same step emptying it), but to go back to your original question, you define empty as "all elements are undef" which is a mighty unusual definition of empty. Surely if it's empty, there are no elements.

You may be confused by the fact that refering to a hash key that does not exist returns undef. If the hash key does exist, but its value is undef, then the hash is not empty - it has at least one key. The 'exists' function is what you'd use to check for the existence of a particular key.

If your hash might have undefined values but you just want to know if there are any defined values, you could say:

if(grep defined($_), values %hash) { ... }

Replies are listed 'Best First'.
Re: Re: Checking and undef'in hash elements in one step?
by Enlil (Parson) on Apr 16, 2003 at 01:36 UTC
    What about a way to do both :-)

    TMTOWTDI (couldn't figure out how to do this with just an "or" or an "and")

    use strict; use warnings; use Data::Dumper; my $hash; $hash->{'a'} = 1; $hash->{'b'} = undef; $hash->{'c'} = undef; $hash->{'d'} = undef; unless (grep {defined $$hash{$_} xor $$hash{$_}=undef} keys %{$hash} ) + { print "empty\n";} else { print "Not empty\n"; } print Dumper \$hash;

    -enlil