Searching an example for a defined hash value of a nonexisting hash key

R

Ralf Baerwaldt

In "man perlfunc" I found:

-----
defined EXPR
....
When used on a hash element, it tells you whether
the value is defined, not whether the key exists
in the hash.
-----

Is it really possible to have a defined value on a
nonexisting key ? Can someone give a sample ?

If I want to print the defined values of a hash, do I have to prove
if the keys exists ? Up to now I just prove for a defined value
assuming that in this case the key must exists, i.e.
 
P

Paul Lalli

In "man perlfunc" I found:

-----
defined EXPR
...
When used on a hash element, it tells you whether
the value is defined, not whether the key exists
in the hash.

No. The point of that manual text is that a key/value pair can exist
without the value being defined.

As relates to Perl hashes, boolean truth implies defined, and defined
implies exists.
If I want to print the defined values of a hash, do I have to prove
if the keys exists ?

No. If the value is defined, it exists.

This is a logical series of steps:
if (exists ($hash{key})) {
print "key exists in hash\n";
if (defined ($hash{key})){
print "key's value is defined in hash\n";
if ($hash{key}){
print "key's value is a true value\n";
} else {
print "key's value is false\n";
}
} else {
print "key's value is not defined, and therefore false\n";
}
} else {
print "key does not exist. Value is therefore undefined and false\n";
}


HTH,
Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top