networktestinggenius.tk
Your networking tutor
Exclusive networking testing notes
Learn Networking & Testing topics

Hashes
Create an empty hash
my %favclor;
Insert a key-value pair into a hash
In this case 'Bhuvana' is the key and 'red' is the associated value.
$favclor{'Bhuvana'} = 'red';
You can also use a variable instead of the key and then you don't need to put the variable in quotes:
my $names= 'Bhuvana';
$favclor{$name} = 'red';
Actually, if the key is a simple string, you could leave out the quotes even when you use the string directly:
$favclor{Bhuvana} = 'red';
As you can see above, when accessing a specific key-value pair, we used the $ sign (and not the % sign) because we are accessing a single value which is a scalar. The key is placed in curly braces.
Fetch an element of a hash
Quite similar to the way we inserted an element, we can also fetch the value of an element.
print $favclor{Bhuvana};
If the key does not exist, the hash will return an undef, and if warnings are enabled, as they should be, then we'll get a warning about uninitialized value.
print $favclor{Thiru};
Let's a few more key-value pairs to the hash:
$favclor{Thiru} = "Thiru";
$favclor{Selvi} = "purple";
Initialize a hash with values
We could have instantiated the variable with the key-value pairs simultaneous passing to the hash a list of key-value pairs:
my %favclor = (
"Bhuvana" => "red",
"Thiru" => "Green",
"Selvi" => "purple",
);
=> is called the fat arrow or fat comma, and it is used to indicate pairs of elements. The first name, fat arrow, will be clear once we see the the other, thinner arrow (->) used in Perl. The name fat comma comes from the fact that these arrows are basically the same as commas. So we could have written this too:
my %favclor = (
"Bhuvana", "red",
"Thiru", "Green",
"Selvi", "purple",
);
Actually, the fat comma allows you to leave out the quotes on the left-hand side making the code cleaner and more readable.
my %favclor = (
Bhuvana => "red",
Thiru => "Green",
Selvi => "purple",
);
Assignment to a hash element
Let's see what happens when we assign another value to an existing key:
$favclor{Bhuvana} = "green";
print $favclor{Bhuvana}; # green
The assignment changed the value associated with the Bhuvana key. Remember, keys are unique and each key has a single value.
Iterating over hashes
In order to access a value in a hash you need to know the key. When the keys of a hash are not pre-defined values you can use the keys function to get the list of keys. Then you can iterate over those keys:
my @names = keys %favclor;
for my $names(@names) {
print "The color of '$name' is $favclor{$name}\n";
}
You don't even need to use the temporary variable @names, you can iterate over directly the return values of the keys function:
for my $names(keys %favclor) {
print "The color of '$name' is $favclor{$name}\n";
}
The size of a hash
When we say the size of a hash, usually we mean the number of key-value pairs. You can get it by placing the keys function in scalar context.
print scalar keys %hash
Using empty values in a hash
A value cannot be "empty" but it can contain undef which what people usually mean when they say "empty".
We can assign undef to a key (new or existing):
$favclor{Raja} = undef;
or when we create the hash:
my %favclor = (
Bhuvana => "red",
Thiru => "Thiru",
Raja => undef,
Selvi => "purple",
);