Perl Hash
Defining a Hash
You've learn in the previous tutorials about scalar and array data type. Scalar variable allows you to store one thing which is scalar. It could be a string or a number. Array variable allows you to store a collection of scalar. You can access each scalar data in array by using indices. An array can hold as many as scalar as you want depends on available memory of your computer. Accessing an element of array often involves sequential access until you find what you need. When number of item in an array is bigger, the performance is degraded also. Perl created hash - before that is it called associative array - to overcome this limitation. Hash is another kind of collective data type. Hash can hold as many as scalar like array but the way you accessing hash element is different. You access hash element in has by using a key. Each has element has two parts: key and value or key-value pair. The key identifies the value of element associated with it. To define a hash variable, you use percentage sign (%) as follows:
-
%websites = (
-
"perltutorial.org" => "Perl tutorial",
-
"Perl.org" => "Perl directory"
-
);
-
In the above example, we defined a hash called %websites. The hash has two elements and each element is defined by key-value pair.
Accessing Hash elements
To access a hash element, you use the key of that element to retrieve the value as follows:
-
print $websites{"perltutorial.org"}; #Perl tutorial
The code snippet prints values associated with key "perltutorial.org". The dollar sign ($) is used instead of percentage sign (%) because hash element is scalar. Curly braces is used surrounded within hash key instead of square bracket in array.
Filling Hash elements
To fill hash elements, you can define hash as above example or use a list as follows:
-
%websites = (
-
"perltutorial.org" ,"Perl tutorial",
-
"Perl.org", "Perl directory"
-
);
Another way to fill the hash element by using key-value pair and assignment operator as follows:
-
$websites{"Perl.com"} = "Perl.com homepage";
-
print $websites{"Perl.com"}; #Perl.com homepage
Testing a key in a Hash
To test whether a key is existed in a hash, you use exist function provided by Perl. Here is the syntax:
-
if ( exists $hash{key} ){
-
#retrieve value here
-
}
For example, we can test whether key "perltutorial.org" exists in %websites hash by using the above pattern:
-
%websites = (
-
"Perltutorial.org" ,"Perl tutorial",
-
"Perl.org", "Perl directory"
-
);
-
-
if(exists $websites{"Perltutorial.org"}){
-
#Perl tutorial
-
print $websites{"Perltutorial.org"};
-
}
Removing keys from a Hash
To remove a single key from hash, you use delete function as follows:
To remove all keys and values from a hash, you just assign hash to an empty list:
Looping through a Hash
To loop through a hash you use foreach statement. First you retrieve all keys of the hash by using keys function. Based on this list you can find all elements of the hash. Here is the syntax:
-
foreach $element (keys %hash){
-
#process hash element here
-
}
For example, we can loop through %websites hash:
-
%websites = (
-
"Perltutorial.org" ,"Perl tutorial",
-
"Perl.org", "Perl directory"
-
);
-
-
foreach $website (keys %websites){
-
print "$website is about $websites{$website}\n";
-
}
-
-
#output:
-
#Perltutorial.org is about Perl tutorial
-
#Perl.org is about Perl directory
Array and Hash
You can assign an hash to an array and vice versa. For example:
-
%websites = (
-
"Perltutorial.org" =>"Perl tutorial",
-
"Perl.org" => "Perl directory"
-
);
-
-
@websites = %websites;
Array @websites now has 4 elements, you can process array elements and then reassign that array to a hash as follows:
Not only that like array you can assign a hash to another hash: