#!/usr/bin/perl
use strict;
use warnings;
my %hash;
undef($hash{"a"});
undef($hash{"b"});
print scalar values %hash; # i need here 0
print scalar keys %hash; # and here 2
thank you, guys!
#!/usr/bin/perl
use strict;
use warnings;
my %hash;
undef($hash{"a"});
undef($hash{"b"});
print scalar values %hash; # i need here 0
print scalar keys %hash; # and here 2
thank you, guys!
undef($hash{"a"});
is equivalent to
$hash{"a"}=undef;
So you add key 'a' with value undef. To delete value from hash use "delete".
delete $hash{"a"};
It is not possible to have different size of 'keys' and 'values' for same hash. You can use grep to filter unwanted elements.
The other answer's point about definedness vs. existence is a very good one, but If you actually do want to know the number of defined values you can always do
print scalar grep { defined $_ } values %hash