FreeKB - Hash in Perl - Getting Started
Hash in Perl - Getting Started

Home > Search


A hash, or more technically an associative array, creates key:value pairs. For example, let's say you want to create the following key:value pairs.

Key Value
Name Jeremy
ID 123456
Occupation Engineer

 

Generally speaking, there are two approaches to create the hash key:value pair. You either create an empty hash and then append values (this is the much more common approach) or you define the hash and the key:value pairs.

 


Following are both valid examples of how to create an empty hash (does not contain any key value pairs).

my %hash;
my %hash = ();

 

An if statement can be used to confirm that the hash is defined.

if (%hash) { print "\%hash is defined\n"; }

 

An if statement can be used to confirm that hash is empty (contains no key).

if (not keys %hash) { print "\%hash is empty (contains no keys)\n"; }

 

Now, key:value pairs can be created in the hash. Or, this could be one line. I just put the key value pairs on new lines for readability.

my %hash = (
  name => Jeremy, 
  id => 123456, 
  occupation => Engineer
);

 

Or, one at a time.

$hash{name}       = "Jeremy";
$hash{id}         = "123456";
$hash{occupation} = "Engineer";

 

An if statement can be used to confirm that the hash is not empty (contains at least one key:value pair).

if (keys %hash) { 
  my $size = keys $hash;
  print "\%hash is not empty (contains $size keys)\n"; 
}

 


Printing

Dumper can be used to display the structure of the hash.

use Data::Dumper;
print Dumper(\%hash);

 

Which will print all of the keys and values in the hash, like this.

$VAR1 = {
           'name' => 'Jeremy',
           'id' => '123456',
           'occupation' => 'Engineer'
        };

 

Or, you can print the value of a key. For example, the following will print the value "Jeremy" in the "name" key.

print $hash{name};

 


Loop through keys / values

You will usually want to loop through the hash. The following foreach loop will print the keys.

foreach my $key (keys %hash) {
    print $key;
}

 

Which will print each key.

name
id
occupation

 

The following foreach loop will print the value associated with each key.

foreach my $key (keys %hash) {
    print $hash{$key};
}

 

Which will print each value.

Jeremy Canfield
123456
Engineer

 

Or like this.

foreach my $value (values %hash) {
    print $value;
}

 

Will print the values.

Jeremy Canfield
123456
Engineer

 

Or like this, to loop through both the keys and the values.

foreach my $key (keys %food) {
  foreach my $value ($hash{$key}) {
    print "$key -> $value\n";
}

 

Will print the following.

name -> Jeremy Canfield
id -> 123456
occupation -> Engineer

 


Arrays

Just like variables, each hash key will contain a single value. If you were to do something like this, $hash{name} will only contain a value of Winston.

$hash{name} = "Jeremy";
$hash{name} = "Winston";

 

You can store the hash in an array. In this example, the @{$hash{name}} array will contain both Jeremy and Winston.

push ( @{$hash{name}}, "Jeremy" );
push ( @{$hash{name}}, "Winston" );

 

You can then use two foreach loops to print the key:value pairs in the array.

foreach my $key (keys %hash) {
  foreach my $value (@{$hash{$key}}) {
    print "$key = $value\n";
  }
}

 

Which will print the following.

name = Jeremy
name = Winston

 

Or use Dumper to display the structure.

use Data::Dumper;
print Dumper(\%hash);

 

Which will produce the following.

$VAR1 = {
          'name' => [
                      'Jeremy',
                      'Winston'
                    ]
         };

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments