FreeKB - Perl Hash Array (append key value pairs)
Perl - Hash Array (append key value pairs)

In this example, employees_key in the hash is an array, as indicated by the [ ] characters. employees_key is empty (contains no values).

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

 

You can count the elements.

my $count = @{$hash{employees_key}};
print "\$count = $count \n";

 

Which will return 0, since employees_key is empty.

$count = 0

 

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

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

 

Which should produce the following.

$VAR1 = {
          'employees_key' => []
        };

 

Let's say you do this.

push @{$hash{employees_key}}, { 'name_key' => 'John Doe', 'department_key' => 'engineering' };
push @{$hash{employees_key}}, { 'name_key' => 'Jane Doe', 'department_key' => 'sales' };

 

Now, employees_key contains key value pairs.

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

 

You can loop through employees_key to print the value associated with a particular key.

foreach my $employees_key (@{$hash{employees_key}}) {
  print "$employees_key->{name_key} \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 = {
          'employees_key' => [ 
                         {
                           'name_key' => 'John Doe'
                         }
                       ]
        };

 

The following will appended a new key value pair to the block that contains John Doe.

foreach my $employees_key (@{$hash{employees_key}}) {
  if ($employee_key->{name_key} eq "John Doe") {
    $employees_key->{department_key} = "engineering";  
  }
}

 

Now the hash looks like this.

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

 


Multidimensional

Let's say the hash is multidimensional. In this example, departments_key array is a child of the employees_key array.

my %hash = ( 'employees_key' => [ { 'departments_key' => [] } ] );

 

Here is the Dumper output.

$VAR1 = {
          'employees_key' => [ 
                         {
                           'departments_key' => []
                         }
                       ]
        };

 

You can loop through the top level key (employees_key) and print the Dumper output.

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

 

Which should produce the following.

$VAR1 = {
          'departments_key' => []
        };

 

Here is how to append key value pairs to the departments_key array.

foreach my $employees_key (@{$hash{employees_key}}) {
  push @{$employees_key->{departments_key}}, { 'department_key' => 'engineering' };
  push @{$employees_key->{departments_key}}, { 'department_key' => 'sales' };
}

 

Which should return the following.

$VAR1 = {
          'employees_key' => [
                               {
                                 'departments_key' => [
                                               {
                                                 'department_key' => 'engineering'
                                               },
                                               {
                                                 'department_key' => 'sales'
                                               }
                                             ],
                               }
                             ]
        };

 

You can loop through the hashes and use Dumper to print only the keys.

foreach my $employees_key (@{$hash{employees_key}}) {
  foreach my $key (keys %$employees_key) {
    print Dumper $key;
  }
}

 

Which should return the following.

$VAR1 = 'department_key';
$VAR1 = 'department_key';

 

Likewise, you can loop through the hashes and use Dumper to print the key and value pairs.

foreach my $employees_key (@{$hash{employees_key}}) {
  foreach my $department (@{$employees_key->{$department_key}}) {
    print Dumper $department;
  }
}

 

Which should return the following.

$VAR1 = {
          'department_key' => 'engineering'
        };
$VAR1 = {
          'department_key' => 'sales'
        };

 

Now that you know that "department_key" is the key, you can print the value associated with the key.

foreach my $employees_key (@{$hash{employees_key}}) {
  foreach my $key (keys %$employees_key) {
    print "$key = ";
  }
  foreach my $department (@{$employees_key->{$department_key}}) {
    print "$department->{department_key} \n";
  }
}

 

Which should return the following.

department_key = engineering
department_key = sales


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 aef0c 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   |