note
manoj_speed
<p>Hashes contain pairs of scalars: the first is the key, the second is the value. The key will be coerced to a string, although the value can be any kind of scalar: string, number, or reference. If a key $key is present in %hash, "exists($hash{$key})" will return true. The value for
a given key can be "undef", in which case $hash{$key} will be "undef" while "exists $hash{$key}" will return true. This corresponds to ($key, "undef") being in the hash. </p>
<p>Pictures help... here's the %hash table:</p>
<p>keys values<br>
+------+------+<br>
| a | 3 |<br>
| x | 7 |<br>
| d | 0 |<br>
| e | 2 |<br>
+------+------+<br>
And these conditions hold<br>
$hash{'a'} is true<br>
$hash{'d'} is false<br>
defined $hash{'d'} is true<br>
defined $hash{'a'} is true<br>
exists $hash{'a'} is true (Perl 5 only)<br>
grep ($_ eq 'a', keys %hash) is true<br>
If you now say<br>
undef $hash{'a'}<br>
your table now reads:
<br>
keys values<br>
+------+------+<br>
| a | undef|<br>
| x | 7 |<br>
| d | 0 |<br>
| e | 2 |<br>
+------+------+<br>
and these conditions now hold; changes in caps:<br>
$hash{'a'} is FALSE<br>
$hash{'d'} is false<br>
defined $hash{'d'} is true<br>
defined $hash{'a'} is FALSE<br>
exists $hash{'a'} is true (Perl 5 only)<br>
grep ($_ eq 'a', keys %hash) is true<br>
Notice the last two: you have an undef value, but a defined key!<br><br>
Now, consider this:<br>
delete $hash{'a'}<br>
your table now reads:<br>
keys values<br>
+------+------+<br>
| x | 7 |<br>
| d | 0 |<br>
| e | 2 |<br>
+------+------+<br>
and these conditions now hold; changes in caps:<br>
$hash{'a'} is false<br>
$hash{'d'} is false<br>
defined $hash{'d'} is true<br>
defined $hash{'a'} is false<br>
exists $hash{'a'} is FALSE (Perl 5 only)<br>
grep ($_ eq 'a', keys %hash) is FALSE<br>
See, the whole entry is gone!<br></p>
751342
751342