Perl | Multidimensional Hashes
Last Updated :
27 Jun, 2019
Improve
-
Prerequisite: Hashes-Basics
- To add another anonymous member to the existing hash:
Syntax: $company{'new_key'} = {'sub_key1' => value1, 'sub_key2' => value2, 'sub_key3' => value3 };
- Access particular value:
Syntax: print $company{"Production"}{"Web"}; # will output "Developer"
- Set value of a particular key:
Syntax: $company{$Production}->{"Web"} = Senior Developer ; # changes Web to Senior Developer
Introduction
Syntax: my %hash = (primary_key => {secondary_key => {sub_sec_key => {...}}});Example: Following example shows a hash of hashes that describes a company organization. The primary keys are the departments, and the nested keys are the employee names. The values then contain the corresponding employee’s job title.
# !/usr/bin/perl
# Perl program to demonstrate
# Multidimensional hash
use strict;
use warnings;
use Data::Dumper qw(Dumper);
# Creating a 2D hash
my %company = ('Sales' => {
'Brown' => 'Manager',
'Smith' => 'Salesman',
'Albert' => 'Salesman',
},
'Marketing' => {
'Penfold' => 'Designer',
'Evans' => 'Tea-person',
'Jurgens' => 'Manager',
},
'Production' => {
'Cotton' => 'Paste-up',
'Ridgeway' => 'Manager',
'Web' => 'Developer',
},
);
# Print the List
print Dumper(\%company);
Output:
In above example, the input of the Dumper function is a reference to a data structure and thus we put a back-slash (\) in front of %company.
Note: The order of the keys is random as hashes do not keep them in any specific order.
Some other operations:
$VAR1 = { 'Marketing' => { 'Evans' => 'Tea-person', 'Jurgens' => 'Manager', 'Penfold' => 'Designer' }, 'Sales' => { 'Smith' => 'Salesman', 'Albert' => 'Salesman', 'Brown' => 'Manager' }, 'Production' => { 'Ridgeway' => 'Manager', 'Web' => 'Developer', 'Cotton' => 'Paste-up' } };
Traversing Multidimensional Hashes
for $key (keys %hash)
{
print "$key: \n";
for $ele (keys %{$hash{$key}})
{
print " $ele: " . $hash{$key}->{$ele} . "\n";
}
}
while (($key, $ele) = each %hash)
{
print "$key: \n";
while (($ele, $sub_ele) = each %$ele)
{
print " $ele = $sub_ele ";
}
print "\n";
}
For
and while
loops:
Example:
# !/usr/bin/perl
# Perl program to demonstrate
# Traversing of
# Multidimensional hash
use strict;
use warnings;
use Data::Dumper qw(Dumper);
my %company = ('Sales' => {
'Brown' => 'Manager',
'Smith' => 'Salesman',
'Albert' => 'Salesman',
},
'Marketing' => {
'Penfold' => 'Designer',
'Evans' => 'Tea-person',
'Jurgens' => 'Manager',
},
'Production' => {
'Cotton' => 'Paste-up',
'Ridgeway' => 'Manager',
'Web' => 'Developer',
},
);
print "Traversing hash using For loop: "."\n";
print "\n";
# traversing hash using for loop
for my $key (keys %company)
{
print "$key: \n";
for my $ele (keys %{$company{$key}})
{
print " $ele: " . $company{$key}->{$ele} . "\n";
}
}
print "\nTraversing hash using while" .
"loop using each keyword: " . "\n";
print "\n";
# traversing hash using each keyword
# and while loop
while ((my $key, my $ele) = each %company)
{
print "$key: \n";
while ((my $ele, my $sub_ele) = each %$ele)
{
print " $ele = $sub_ele ";
}
print "\n";
}
Output:
Traversing hash by For loop. Marketing: Evans: Tea-person Jurgens: Manager Penfold: Designer Sales: Brown: Manager Albert: Salesman Smith: Salesman Production: Cotton: Paste-up Web: Developer Ridgeway: Manager Traversing hash using while loop with each keyword. Marketing: Evans=Tea-person Jurgens=Manager Penfold=Designer Sales: Brown=Manager Albert=Salesman Smith=Salesman Production: Cotton=Paste-up Web=Developer Ridgeway=Manager
Check for key existence in Multidimensional Hashes
exists
keyword.
In a multidimensional hash like %company used in above examples, one has to use the keyword exists
up until the depth level of the key being checked for existence, has been reached.
Syntax: if (exists($hash{key})) { if (exists($hash{key}{sub_key})) { .... } }One should also be careful using the nth-level construct without trying the (n-1)th-level first as that might trigger unwanted autovivification Example: Here's a simple example that demonstrates the Perl
exists
hash function. In this Perl script, we'll first create a simple Perl hash, and then we'll use the exists
function to see if the hash key named 'Albert' exists in the hash.
# !/usr/bin/perl
# Perl program to check for
# existence of a key in a
# Multidimensional hash
use strict;
use warnings;
my %company = ('Sales' => {
'Brown' => 'Manager',
'Smith' => 'Salesman',
'Albert' => 'Salesman',
},
'Marketing' => {
'Penfold' => 'Designer',
'Evans' => 'Tea-person',
'Jurgens' => 'Manager',
},
'Production' => {
'Cotton' => 'Paste-up',
'Ridgeway' => 'Manager',
'Web' => 'Developer',
},
);
# Check for key existence
if (exists $company{"Sales"})
{
print "Sales department exists.\n";
if (exists $company{"Sales"}{"Albert"})
{
print "Albert is " . $company{"Sales"}{"Albert"} .
" of Sales department . \n";
}
else
{
print "Albert is not a member of Sales department.\n";
}
}
else
{
print "Sales department do not exists.\n";
}
Output:
Sales department exists. Albert is Salesman of Sales department.