variables beginner

How to delete a hash key in Perl?

Question

How to delete a hash key in Perl?

In Perl, deleting a key from a hash is straightforward using the built-in delete function. This function removes a specified key and its associated value from the hash, effectively modifying the hash in place.

How delete Works

The syntax is:

delete $hash{key};

This deletes the entry for key in the hash %hash. If the key exists, it returns the value associated with the key before deletion. If the key does not exist, it returns undef.

Key points

  • Deleting a key actually removes it from the hash, so exists $hash{key} will then return false.
  • You can delete multiple keys by calling delete multiple times or use a map or loop construct.
  • Handles the case gracefully if the key is not present.
  • Works since very early Perl versions; no version constraints.
  • Remember Perl hashes are unordered collections, so deleting keys simply removes those pairs.

Perl Sigils and Context

Note that hash elements are accessed with the $ sigil followed by braces and the key string: $hash{key}. Although %hash represents the whole hash, the $ indicates a single scalar value—the hash element. This distinction is essential for correct code and avoiding syntax errors.

Example: Deleting a Key from a Hash

use strict;
use warnings;

# Define a hash with three keys
my %fruits = (
    apple  => 3,
    banana => 5,
    orange => 2,
);

# Print the hash before deletion
print "Before deletion:\n";
foreach my $key (sort keys %fruits) {
    print "$key => $fruits{$key}\n";
}

# Delete the 'banana' key
my $removed_value = delete $fruits{banana};
print "\nDeleted 'banana', which had value: $removed_value\n";

# Print the hash after deletion
print "\nAfter deletion:\n";
foreach my $key (sort keys %fruits) {
    print "$key => $fruits{$key}\n";
}

# Attempt to delete a non-existing key
my $missing_value = delete $fruits{grape};
print "\nTrying to delete 'grape' (non-existent) returns: " . (defined $missing_value ? $missing_value : 'undef') . "\n";

When this script is run, it will:

  • Print the full hash contents initially.
  • Delete the key banana and show the removed value.
  • Print the hash again without banana.
  • Show what happens when deleting a non-existent key (undef is returned).

Common Pitfalls

  • Confusing array and hash syntax: remember %hash is the entire hash, while $hash{key} accesses one value.
  • Not checking if the key exists before delete (though usually unnecessary).
  • Expecting the keys to be in order—hashes have no guaranteed ordering.

Deleting hash keys in Perl is a simple but powerful operation useful for data cleanup or conditional removal of entries. The delete function is efficient and idiomatic for this purpose.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 6ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
(empty)
STDERR
(empty)

Was this helpful?

Related Questions