
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 = { };
In this example, the foo key in the hash is an array, as indicated by the [ ] characters. The foo key is empty (contains no values).
my %hash = ( 'foo' => [] );
Dumper can be used to display the structure of the hash.
use Data::Dumper;
print Dumper \%hash;
Which should produce the following.
$VAR1 = {
'foo' => []
};
Let's say you do this.
push @{$hash{foo}}, { 'name' => 'John Doe', 'department' => 'engineering' };
push @{$hash{foo}}, { 'name' => 'Jane Doe', 'department' => 'sales' };
Now, the foo key contains key value pairs.
$VAR1 = {
'foo' => [
{
'name' => 'John Doe',
'department' => 'engineering'
},
{
'name' => 'Jane Doe',
'department' => 'sales'
}
]
};
You can loop through the foo key to print the value associated with the key.
foreach my $foo (@{$hash{foo}}) {
print "$foo->{name} \n";
}
Which in this example will print the value of the name key.
John Doe
Jane Doe
Let's say the hash already contains a key value pair, like this, and you want to append another key value pair at the same place in the hash.
$VAR1 = {
'foo' => [
{
'foo' => 'John Doe'
}
]
};
The following will appended a new key value pair to the block that contains John Doe.
foreach my $foo (@{$hash{foo}}) {
if ($foo->{name} eq "John Doe") {
$foo->{department} = "engineering";
}
}
Now the hash looks like this.
$VAR1 = {
'foo' => [
{
'name' => 'John Doe',
'department' => 'engineering'
}
]
};
Let's say you want to create an array like this, where the bar key is a child of the foo key, meaning the hash is multidimensional.
$VAR1 = {
'foo' => [
{
'bar' => [
{
'name' => 'John Doe',
'role' => 'Engineer'
}
]
}
]
};
AVOID TROUBLE
The hash must be defined before key value pairs can be appended to the bar key.
Define the hash.
my %hash = ( 'foo' => [ { 'bar' => [] } ] );
Dumper can be used to display the structure of the hash.
use Data::Dumper;
print Dumper \%hash;
Which should return something like this.
$VAR1 = {
'foo' => [
{
'bar' => []
}
]
};
You can loop through the top level key (foo) and print the Dumper output.
foreach my $foo (@{$hash{foo}}) {
print Dumper $foo;
}
Which should produce the following.
$VAR1 = {
'bar' => []
};
Here is how to append key value pairs to the bar array.
foreach my $foo (@{$hash{foo}}) {
push @{$foo->{bar}}, { 'name' => 'John Doe', 'role' => 'Engineeer' };
}
Now Dumper should return the following.
$VAR1 = {
'foo' => [
{
'bar' => [
{
'name' => 'John Doe',
'role' => 'Engineer'
}
]
}
]
};
You can loop over the foo key.
foreach my $foo (@{$hash{foo}}) {
print Dumper $foo;
}
Which should return the following.
$VAR1 = {
'bar' => [
{
'name' => 'John Doe',
'role' => 'Engineer'
}
]
};
You can loop over the parent key (foo) and print the child keys (bar).
foreach my $foo (@{$hash{foo}}) {
foreach my $key (keys %$foo) {
print Dumper $key;
}
}
Which should return the following.
$VAR1 = 'bar';
You can loop over the parent key (foo) and print the values from the child key (bar).
foreach my $foo (@{$hash{foo}}) {
foreach my $value (values $foo) {
print Dumper $value;
}
}
Which should return the following.
$VAR1 = [
{
'name' => 'John Doe',
'role' => 'Engineer'
}
];
Or like this.
foreach my $foo (@{$hash{foo}}) {
foreach my $value (@{$foo->{bar}}) {
print "$value->{name}\n";
print "$value->{role}\n";
}
}
Which should return the following.
John Doe
Engineer
Did you find this article helpful?
If so, consider buying me a coffee over at