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

In this example, %hash contains a single key (foo), and the key is an empty array, as indicated by the [ ] characters.

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


You can count the elements.

my $count = @{$hash{foo}};
print "\$count = $count \n";


Which will return 0, since foo is empty.

$count = 0


Let's say you do this.

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


Or this.

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


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

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


Now, the foo key contains two values, John Doe and Jane Doe.

$VAR1 = {
          'foo' => [ 
                     'John Doe', 
                     'Jane Doe'


You can loop through the values in the foo key.

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


Should return the following.

John Doe
Jane Doe


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 fe454 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |