June 2005
Beginner
480 pages
10h 31m
English
Some operations on hashes aren't obvious if you're new to Perl. Because of the special nature of hashes, a couple of common operations require functions that aren't necessary for scalars and arrays.
To test to see whether a key exists in a hash, for example, you might be tempted to try the following syntax:
if ( $Hash{keyval} ) { # WRONG, in this case
:
}
This example doesn't work, for a few reasons. First, this snippet doesn't test to see whether keyval is a key in a hash; it actually tests the value associated with the key keyval in the hash.
Does it work to test whether the key is defined, as in the following?
if ( defined $Hash{keyval} ) { # WRONG, again in this case
:
}
Again, this example ...
Read now
Unlock full access