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

In Perl, there are 3 different kinds of hashes.

  • A regular hash, which is defined by the % and ( ) characters - %hash = ( );
  • A reference hash, which is defined with the $ and { } characters = $hash = { };
  • A hash array, which is defined with the % and [ ] characters - %hash = [ ];

Let's say you define a hash array named %hash that contains a single key (foo). The foo key will contain an array of values. The [ ] characters are used to set the foo key as an array.

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

 

The following can be used to print the number of values in the foo key.

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

 

Which should return the following.

values = 0

 

Let's say the foo key contains 2 values.

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

 

Now, the following should be returned.

values = 2

 



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 202a5 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   |