views:

117

answers:

2
#!/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!

+5  A: 
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.

Alexandr Ciornii
sorry for being not descriptive in my question, now i guess is more clear. thank you.
Alexandr
+1  A: 

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
hobbs
Surely you mean `values %hash`.
darch
Of course I did. Thanks for catching it :)
hobbs
yeap, that's exactly what i need!
Alexandr