Bootstrap FreeKB - Perl (Scripting) - Loop through keys in an array in a hash
Perl (Scripting) - Loop through keys in an array 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 a hash named %hash which contains two keys, foo and bar. Both keys are arrays, as indicated by the [ ] characters.

my %hash = ( 'foo' => [ 'John Doe', 'Jane Doe' ], 'bar' => [ 'Engineer', 'Sales' ] );

 

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

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

 

Which should return something like this.

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

 

Here is how you would loop through the top level keys.

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

 

Which should return the following.

foo
bar

 


Let's say you a hash named %hash where the foo and bar keys are children of the primary key, which means the hash is multidimensional. Both the foo and bar keys are arrays, as indicated by the [ ] characters.

my %hash = ( 'primary' => { 'foo' => [ 'John Doe', 'Jane Doe' ], 'bar' => [ 'Engineer', 'Sales' ] } );

 

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

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

 

Which should return something like this.

$VAR1 = {
          'primary' => {
                         'bar' => [
                                    'Engineer',
                                    'Sales'
                                  ],
                         'foo' => [
                                    'John Doe',
                                    'Jane Doe'
                                  ]
                       }
        };

 

Here is how you would loop through the keys below the primary key.

foreach my $key (keys $hash{primary}) {
  print "$key \n";
}

 

Which should return the following.

foo
bar

 


Let's consider this slight variation. In this example, the primary, foo and bar keys are all arrays.

my %hash = ( 'primary' => [ { 'foo' => [ { 'bar' => [ 'John Doe', 'Jane Doe' ] } ] } ] );

 

Dumper should return something like this.

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

 

Here is how you would get the keys below the primary key array.

foreach my $primary ( @{$hash{primary}} ) {
  foreach my $primary_keys ( keys %$primary) {
    print "The following keys are children of 'primary' = $primary_keys \n";
    foreach my $foo ( @{$primary->{$primary_keys}} ) {
      foreach my $foo_keys ( keys %$foo ) {
        print "The following keys are children of '$primary_keys' = $foo_keys \n";
      }
    }
  }
}

 

Which should return the following.

The following keys are children of 'primary': foo
The following keys are children of 'foo': 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 000ce7 in the box below so that we can be sure you are a human.