Bootstrap FreeKB - Perl (Scripting) - Append values to a hash that contains an array
Perl (Scripting) - Append values to a hash that contains an array

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 = { };

 

The first step in creating a hash that contains an array is to define a hash or define a reference hash that has at least one key that contains an array


Append values to a Hash

Here is how you would define an empty hash named %hash where the foo key contains an array. The [ ] characters are used to set the foo key as an array.

my %hash = ( 'foo' => [] );

 

Let's say you do this.

%hash = ( 'foo' => [ 'John Doe','Jane Doe' ] );

 

Or this.

push @{$hash{foo}}, "John Doe";
push @{$hash{foo}}, "Jane Doe";

 

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

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

 

Now, the foo key contains two values, John Doe and Jane Doe.

$VAR1 = {
          'foo' => [ 
                     'John Doe', 
                     'Jane Doe'
                   ]
        };

 

You can loop through the values in the foo key.

foreach my $foo (@{$hash{foo}}) {
  print "$foo \n";
}

 

Should return the following.

John Doe
Jane Doe

 

 


Append values to a Reference Hash

Here is how you would define an empty reference hash named $hash where the foo key contains an array. The [ ] characters are used to set the foo key as an array.

my $hash = { 'foo' => [] };

 

Let's say you do this.

$hash = { 'foo' => [ 'John Doe','Jane Doe' ] };

 

Or this.

push @{$hash->{foo}}, "John Doe";
push @{$hash->{foo}}, "Jane Doe";

 

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

use Data::Dumper;
print Dumper $hash;

 

Now, the foo key contains two values, John Doe and Jane Doe.

$VAR1 = {
          'foo' => [ 
                     'John Doe', 
                     'Jane Doe'
                   ]
        };

 

You can loop through the values in the foo key.

foreach my $foo (@{$hash->{foo}}) {
  print "$foo \n";
}

 

Should return the following.

John Doe
Jane Doe

 


Let's say you want to create an array like this, where the bar key is a child of the foo key, meaning this has would be multidimensional.

$VAR1 = {
          'foo' => [ 
                     {
                       'bar' => [
                                  'Hello',
                                  'World'
                                ]
                     }
                   ]
        };

 

AVOID TROUBLE

The hash must be defined before values 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 values to the bar array.

foreach my $foo (@{$hash{foo}}) {
  push @{$foo->{bar}}, 'Hello';
  push @{$foo->{bar}}, 'World';
}

 

Now Dumper should return the following.

$VAR1 = {
          'foo' => [
                     {
                       'bar' => [
                                  'Hello',
                                  'World'
                                ]
                     }
                   ]
        };

 

You can loop over the foo key.

foreach my $foo (@{$hash{foo}}) {
  print Dumper $foo;
}

 

Which should return the following.

$VAR1 = {
          'bar' => [
                     'Hello',
                     'World'
                   ]
        };

 

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 = [
          'Hello',
          'World'
        ];

 

Or like this.

foreach my $foo (@{$hash{foo}}) {
  foreach my $value (@{$foo->{bar}}) {
    print "$value\n";
  }
}

 

Which should return the following.

Hello
World

 




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 1adf11 in the box below so that we can be sure you are a human.