Bootstrap FreeKB - Perl (Scripting) - Delete a key in a hash
Perl (Scripting) - Delete a key in a hash

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter ec9d41 in the box below so that we can be sure you are a human.