FreeKB - Hash Array (create empty array)
Perl - Hash Array (create empty array)

Let's say you do the following. In this example, the name of the hash is %hash and the hash contains a key named employees_key. employees_key is an empty array, as indicated by the [ ] characters.

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

 

You could use Dumper to display the structure.

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

 

employees_key is empty.

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

 


Multiple arrays

Let's say you do this.

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

 

Now the hash contains two empty arrays.

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

 


Multidimensional arrays

Let's say you do this.

push @{$hash{employees_key}}, { 'departments_key' => [] };

 

Now the hash contains two arrays, the employees_key array and the departments_key array, where the departments_key array is a child of the employees_key array. The departments_key array is empty. However, the employees_key array is not empty, since the employees_key array contains departments_key.

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

 

Let's say you do this.

push @{$hash{employees_key}}, { 'main' => { 'departments_key' => [] } };

 

Now, below employees_key array is the main_key hash, and below the main"_key hash is the departments_key array.

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

 


Don't do this

Let's say you do this.

push @{$hash{employees_key}}, "departments_key";

 

Now the employees_key array contains a value. The value is departments_key.  This is outside of the scope of creating an empty array. Instead, this is how you append values to an array.

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


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 5c308 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   |