FreeKB - Hash with multiple Arrays (multidimensional)
Perl - Hash with multiple Arrays (multidimensional)

This assumes you know how to create a hash with one array, like this.

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

 

Or this.

push ( @{$hash{employees}}, "John Doe" );
push ( @{$hash{employees}}, "Jane Doe" );

 

You could use Dumper to display the structure.

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

 

The hash contains one array (employees), and the array contains two values (John Doe and Jane Doe). 

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

 

Looping through the employees key . . .

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

 

. . . will print the employees.

John Doe
Jane Doe

 

 


Arrays at the same level

Let's say you do this.

my %hash = ( employees => [ 'John Doe','Jane Doe' ], departments => [ 'engineering', 'sales'] );

 

Or this.

push ( @{$hash{employees}}, "John Doe" );
push ( @{$hash{employees}}, "Jane Doe" );
push ( @{$hash{departments}}, "engineering" );
push ( @{$hash{departments}}, "sales" );

 

The hash now contains two arrays, employees and departments.

$VAR1 = {
          'employees' => [
                           'John Doe',
                           'Jane Doe'
                         ],
          'departments' => [
                             'engineering',
                             'sales'
                           ]
        };

 

Looping through the employees key . . .

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

 

. . . will print the values.

John Doe
Jane Doe

 

Looping through the departments key . . .

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

 

. . . will print the departments.

engineering
sales

 

 


Multidimensional arrays

Let's say you do this.

push ( @{$hash{employees}}, { name => "Jane Doe", department => "sales", badges => [] } );

 

Now the hash contains two arrays, the "employees" array and the "badges" array.

$VAR1 = {
          'employees' => [ 
                     {
                       'name' => 'Jane Doe',
                       'department' => 'sales',
                       'badges' => []
                     }
                   ]
        };

 

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

foreach my $employee (@{$hash{"employees"}}) {
  push ( @{$employee->{badges}}, { badge => "111111" } );
  push ( @{$employee->{badges}}, { badge => "222222" } );
}

 

Which should return the following.

$VAR1 = {
          'employees' => [
                           {
                             'badges' => [
                                           {
                                             'badge' => '111111'
                                           },
                                           {
                                             'badge' => '222222'
                                           }
                                         ],
                             'department' => 'engineering',
                             'name' => 'John Doe'
                           }
                         ]
        };

 



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




Comments