Perl (Scripting) - Delete a key in a hash

by
Jeremy Canfield |
Updated: August 10 2022
| Perl (Scripting) articles
In Perl, there are 2 different kinds of hashes.
- A hash, which is defined by the % and ( ) characters - %hash = ( );
- A reference hash, which is defined with the $ and { } characters - $hash = { };
Let's say you have the following hash. In this example, there are two keys, foo and bar.
my %hash = ( 'foo' => 'Hello', 'bar' => 'World');
Dumper can be used to display the structure of the hash.
use Data::Dumper;
print Dumper \%hash;
Which shows that the employee key contains a value of John Doe.
$VAR1 = {
'foo' => 'Hello',
'bar' => 'World'
};
Here is how you would delete the bar key.
delete $hash{bar};
Did you find this article helpful?
If so, consider buying me a coffee over at