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:

  1. %websites = (
  2.   "perltutorial.org" => "Perl tutorial",
  3.   "Perl.org" => "Perl directory"
  4. );
  5.  

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:

  1. 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:

  1. %websites = (
  2.   "perltutorial.org" ,"Perl tutorial",
  3.   "Perl.org", "Perl directory"
  4. );

Another way to fill the hash element by using key-value pair and assignment operator as follows:

  1. $websites{"Perl.com"} = "Perl.com homepage";
  2. 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:

  1. if ( exists $hash{key} ){
  2.   #retrieve value here
  3. }

For example, we can test whether key "perltutorial.org" exists in %websites hash by using the above pattern:

  1. %websites = (
  2.   "Perltutorial.org" ,"Perl tutorial",
  3.   "Perl.org", "Perl directory"
  4. );
  5.  
  6. if(exists $websites{"Perltutorial.org"}){
  7.    #Perl tutorial
  8.    print $websites{"Perltutorial.org"};
  9. }

Removing keys from a Hash

To remove a single key from hash, you use delete function as follows:

  1. delete $hash{key};

To remove all keys and values from a hash, you just assign hash to an empty list:

  1. %hash = ();

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:

  1. foreach $element (keys %hash){
  2.    #process hash element here
  3. }

For example, we can loop through %websites hash:

  1. %websites = (
  2.   "Perltutorial.org" ,"Perl tutorial",
  3.   "Perl.org", "Perl directory"
  4. );
  5.  
  6. foreach $website (keys %websites){
  7.    print "$website is about $websites{$website}\n";
  8. }
  9.  
  10. #output:
  11. #Perltutorial.org is about Perl tutorial
  12. #Perl.org is about Perl directory

Array and Hash

You can assign an hash to an array and vice versa. For example:

  1. %websites = (
  2.   "Perltutorial.org" =>"Perl tutorial",
  3.   "Perl.org" => "Perl directory"
  4. );
  5.  
  6. @websites = %websites;

Array @websites now has 4 elements, you can process array elements and then reassign that array to a hash as follows:

  1. %websites = @websites;

Not only that like array you can assign a hash to another hash:

  1. %dir = %websites;