FreeKB - Perl (Scripting) loop through hash values
Perl (Scripting) - loop through hash values

Let's say you create a hash named %hash, and you append a hash key value pair (department_key => engineering) and you also append an array of values (employees_key => John Doe and Jane Doe), like this.

my %hash = ( 'department_key' => 'engineering', 'employees_key' => [ 'John Doe', 'Jane Doe' ] );

 

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

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

 

Which should produce the following.

$VAR1 = {
          'department_key' => 'engineering',
          'employees_key' => [
                               'John Doe',
                               'Jane Doe'
                             ]
        };

 

You can loop through department_key . . .

foreach my $value ($hash{department_key}) {
  print "$value \n";
}

 

. . . and print the values.

engineering

 

You can loop through employees_key . . .

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

 

. . . and print the values.

John Doe
Jane Doe

 


key value pairs

In this example, the employees_key array contains key value pairs.

$VAR1 = {
          'employees_key' => [ 
                              {
                               'name_key' => 'John Doe', 
                               'department_key' => 'engineering'
                              }
                             ]
        };

 

If you do this . . .

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

 

Something like this will be returned.

HASH(0x2fd9bc8) 
HASH(0x5ac1b49)

 

When looping through an array that contains key value pair, you need to specify the keys, like this.

foreach my $value (@{$hash{employees_key}}) {
  print "$value->{name_key} \n";
  print "$value->{department_key} \n";
}

 

Which should print the values.

John Doe
engineering

 


Multidimensional

Let's say you have the following multidimensional hash.

my %hash = ( 'tomcat' => [ { 'dev' => [ { 'engineering' => [ { 'foo' => 'Hello', 'bar' => 'World' } ] } ] } ] );

 

Here is the Dumper output.

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

 

Here is how you could loop through the hash, printing the keys (tomcat, dev, engineering, foo, bar) and values (Hello World).

foreach my $platform (keys %hash) {
  print "platform key = $platform \n";
  foreach my $environment_key (@{$hash{$platform}}) {
    foreach my $environment (keys %$environment_key) {
      print "environment key = $environment \n";
      foreach my $department_key (@{$environment_key->{$environment}}) {
        foreach my $department (keys %$department_key) {
          print "department key = $department \n";
        }
        foreach my $data (@{$department_key->{$department}}) {
          foreach my $data_key (keys %$data) {
            if ($data_key eq "foo") {
              print "foo key contains a value of : $data->{foo} \n";
            }
            if ($data_key eq "bar") {
              print "bar key contains a value of : $data->{bar} \n";
            }
          }
        }
      }
    }
  }
}

 

Which should return the following.

platform key = tomcat
environment key = dev
department key = engineering
foo key contains a value of : Hello
bar key contains a value of : World

 



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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |