|
Posted by PerlFAQ Server on March 7, 2008, 3:03 pm
Please log in for more thread options
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)" will return true. The value for
a given key can be "undef", in which case $hash will be "undef"
while "exists $hash" 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 is true
$hash is false
defined $hash is true
defined $hash is true
exists $hash is true (Perl 5 only)
grep ($_ eq 'a', keys %hash) is true
If you now say
undef $hash
your table now reads:
keys values
+------+------+
| a | undef|
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+
and these conditions now hold; changes in caps:
$hash is FALSE
$hash is false
defined $hash is true
defined $hash is FALSE
exists $hash is true (Perl 5 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
your table now reads:
keys values
+------+------+
| x | 7 |
| d | 0 |
| e | 2 |
+------+------+
and these conditions now hold; changes in caps:
$hash is false
$hash is false
defined $hash is true
defined $hash is false
exists $hash is FALSE (Perl 5 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.
|
| Similar Threads | Posted | | FAQ 4.61: What's the difference between "delete" and "undef" with hashes? | November 15, 2004, 12:03 pm |
| FAQ 4.61 What's the difference between "delete" and "undef" with hashes? | January 17, 2005, 12:03 pm |
| FAQ 4.61 What's the difference between "delete" and "undef" with hashes? | May 17, 2005, 11:03 am |
| FAQ 4.61 What's the difference between "delete" and "undef" with hashes? | August 2, 2005, 10:03 am |
| FAQ 4.61 What's the difference between "delete" and "undef" with hashes? | September 27, 2005, 10:03 am |
| FAQ 4.61 What's the difference between "delete" and "undef" with hashes? | February 14, 2006, 6:03 pm |
| FAQ 4.61 What's the difference between "delete" and "undef" with hashes? | July 29, 2006, 9:03 am |
| FAQ 4.61 What's the difference between "delete" and "undef" with hashes? | October 17, 2006, 3:03 am |
| FAQ 4.61 What's the difference between "delete" and "undef" with hashes? | December 25, 2006, 9:03 pm |
| FAQ 4.61 What's the difference between "delete" and "undef" with hashes? | May 1, 2007, 3:03 pm |
|