Programming Perl

Programming PerlSearch this book
Previous: 3.2.25 definedChapter 3
Next: 3.2.27 die

3.2.26 delete

delete EXPR

This function deletes the specified key and associated value from the specified hash. (It doesn't delete a file. See unlink for that.) Deleting from $ENV{} modifies the environment. Deleting from a hash that is bound to a (writable) DBM file deletes the entry from the DBM file.

The following naïve example inefficiently deletes all the values of a hash:

foreach $key (keys %HASH) {
    delete $HASH{$key};

(It would be faster to use the undef command on the whole hash.) EXPR can be arbitrarily complicated as long as the final operation is a hash key lookup:

delete $ref->[$x][$y]{$key};

For normal hashes, the delete function happens to return the value (not the key) that was deleted, but this behavior is not guaranteed for tied hashes, such as those bound to DBM files.

To test whether a hash element has been deleted, use exists.

Previous: 3.2.25 definedProgramming PerlNext: 3.2.27 die
3.2.25 definedBook Index3.2.27 die