FreeKB - Perl Hash Array (determine if hash array defined)
Perl - Hash Array (determine if hash array defined)

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

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

 

Or like this.

my %hash;
push @{$hash{employees_key}};

 

Dumper can be used to display the structure.

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

 

Which will display the data structure.

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

 

The following if statement will return "%hash is defined". This shows that a hash becomes defined once the hash contains a key.

if (%hash) {
  print "\%hash is defined\n";
}

 

Let's say you append a key value pair to the array.

push @{$hash{employees_key}}, { 'name_key' => 'John Doe' };

 

Now, employees_key contains a key value pair.

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

 

The following will return $hash{employees_key} is not empty because employees_key contains a key or value.

foreach my $employees_key (@{$hash{employees_key}}) {
  if ($employees_key) {
    print "\$hash{employees_key} is not empty \n";
  }
}

 

 



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 2b5e8 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   |