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

In this example, employees_key is an array that contains two values, John Doe and Jane Doe.

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

 

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

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

 

Which shows that the employees_key array contains two values, John Doe and Jane Doe.

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

 

All of the values in the employees_key array can be removed, like this.

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

 

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

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

 

Which shows that the employees_key array is now empty.

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

 


Multidimensional array

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

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

 

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

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

 

Which shows that department_key contains now values, engineering and sales.

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

 

Here is now to undefine departments_key.

foreach my $employees_key (@{$hash{employees_key}}) {
  undef $employees_key->{departments_key};
}

 

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

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

 

Which shows that departments_key is now undefined.

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


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