Perl (Scripting) - Count keys and values in a hash
by Jeremy Canfield | Updated: August 10 2022 | Perl (Scripting) articles
In Perl, there are 2different kinds of hashes. Empty hash Let's say you have the following empty hash that contains no keys and no values. The following can be used to print the number of keys and values in the hash. Which should return the following. Hash with keys and values Let's append some keys and values. In this example, the hash has three keys (employee, id,department) and three values (John Doe, 123456, Engineering) Print the number of keys and values in the hash. Which should return the following.my %hash;
my $keys = keys %hash;my $values = values %hash;print "keys = $keys \n";print "values = $values \n";
keys = 0values = 0
my %hash;$hash{employee} = "John Doe";$hash{id} = "123456";$hash{department} = "Engineering";
my $keys = keys %hash;my $values = values %hash;print "keys = $keys \n";print "values = $values \n";
keys = 3values = 3
Did you find this article helpful?
If so, consider buying me a coffee over at