FreeKB - 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";
}

 

The following will return "$hash{employees_key} contains no values".

foreach my $employees_key (@{$hash{employees_key}}) {
  if ($employees_key) {
    print "\$hash{employees_key} contains no values\n";
  }
  else {
    print "\$hash{employees_key} contains values\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 3f254 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   |