FAQ 4.61 What's the difference between "delete" and "undef" with hashes?



This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.61: What's the difference between "delete" and "undef" with hashes?

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.

Pictures help... here's the %hash table:

keys values
+------+------+
| a | 3 |
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+

And these conditions hold

$hash{'a'} is true
$hash{'d'} is false
defined $hash{'d'} is true
defined $hash{'a'} is true
exists $hash{'a'} is true (Perl5 only)
grep ($_ eq 'a', keys %hash) is true

If you now say

undef $hash{'a'}

your table now reads:

keys values
+------+------+
| a | undef|
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+

and these conditions now hold; changes in caps:

$hash{'a'} is FALSE
$hash{'d'} is false
defined $hash{'d'} is true
defined $hash{'a'} is FALSE
exists $hash{'a'} is true (Perl5 only)
grep ($_ eq 'a', keys %hash) is true

Notice the last two: you have an undef value, but a defined key!

Now, consider this:

delete $hash{'a'}

your table now reads:

keys values
+------+------+
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+

and these conditions now hold; changes in caps:

$hash{'a'} is false
$hash{'d'} is false
defined $hash{'d'} is true
defined $hash{'a'} is false
exists $hash{'a'} is FALSE (Perl5 only)
grep ($_ eq 'a', keys %hash) is FALSE

See, the whole entry is gone!



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Re: Suggestion for an AES Based Hash Function
    ... amateurs posting here seem to favour cyphers, I go for hash functions. ... Rather than trying to use a strong one-way compression function would ... I have used a fixed set of keys in two places. ... preferred to have the first round keys fixed and to start deriving ...
    (sci.crypt)
  • FAQ 4.71 How can I check if a key exists in a multilevel hash?
    ... How can I check if a key exists in a multilevel hash? ... The trick to this problem is avoiding accidental autovivification. ... subroutine can tell you not only if the keys exist but also get the ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ...
    (comp.lang.perl.misc)
  • FAQ 4.71 How can I check if a key exists in a multilevel hash?
    ... How can I check if a key exists in a multilevel hash? ... The trick to this problem is avoiding accidental autovivification. ... subroutine can tell you not only if the keys exist but also get the ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ...
    (comp.lang.perl.misc)
  • Suggestion for an AES Based Hash Function
    ... amateurs posting here seem to favour cyphers, I go for hash functions. ... Rather than trying to use a strong one-way compression function would ... The round function ... I have used a fixed set of keys in two places. ...
    (sci.crypt)
  • FAQ 4.56 How do I merge two hashes?
    ... How do I merge two hashes? ... both hashes contain keys that are the same and if you want to leave the ... new hash, then add the keys from the other hash (%hash2 to ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ...
    (comp.lang.perl.misc)