FreeKB - Perl Hash Array (append values)
Perl - Hash Array (append values)

In this example, employees_key in the hash is an empty array, as indicated by the [ ] characters.

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

 

Let's say you do this.

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

 

Or this.

push @{$hash{employees_key}}, "John Doe";
push @{$hash{employees_key}}, "Jane Doe";

 

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

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

 

Now, employees_key array contains two values, John Doe and Jane Doe.

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

 

Looping through employees_key . . .

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

 

. . . will print the values.

John Doe
Jane Doe

 


Let's say you do this.

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

 

Or this.

push @{$hash{employees_key}}, "John Doe";
push @{$hash{employees_key}}, "Jane Doe";
push @{$hash{departments_key}}, "engineering";
push @{$hash{departments_key}}, "sales";

 

The hash now contains two arrays, employees_key and departments_key.

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

 

Looping through employees_key . . .

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

 

. . . will print the employees.

John Doe
Jane Doe

 

Looping through departments_key . . .

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

 

. . . will print the departments.

engineering
sales

 


Multidimensional array

In this example, the departments_key array is a child of the employees_key array.

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

 

Here is how to append values to departments_key array.

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

 

Which should return the following.

$VAR1 = {
          'employees_key' => [
                              {
                               'departments_key' => [
                                                     'engineering',
                                                     '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 e4e7a 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   |