Perl Hashes
A hash is a collection of **key/value** pairs.
In Perl, hash variables are denoted by a percent sign (%).
The format to access a hash element is: **${key}**.
Here is a simple hash example:
## Example
#!/usr/bin/perl%data = ('google', 'google.com', 'tutorial', '', 'taobao', 'taobao.com'); print"$data{'google'} = $data{'google'}n"; print"$data{'tutorial'} = $data{'tutorial'}n"; print"$data{'taobao'} = $data{'taobao'}n";
Executing the above program gives the following output:
!(#)
* * *
## Creating a Hash
A hash can be created in the following two ways:
### 1. Setting a value for each key
$data{'google'} = 'google.com'; $data{'tutorial'} = ''; $data{'taobao'} = 'taobao.com';
### 2. Using a list
The first element in the list is the key, the second is the value.
%data = ('google', 'google.com', 'tutorial', '', 'taobao', 'taobao.com');
You can also use the **=>** symbol to set key/value pairs:
%data = ('google'=>'google.com', 'tutorial'=>'', 'taobao'=>'taobao.com');
The following example is a variation of the above, using **-** instead of quotes:
%data = (-google=>'google.com', -tutorial=>'', -taobao=>'taobao.com');
When using this method, the key cannot contain spaces. The way to read an element is:
$val = $data{-google} $val = $data{-tutorial}
* * *
## Accessing Hash Elements
The format to access a hash element is: **${key}**, as shown in the following example:
## Example
#!/usr/bin/perl%data = ('google'=>'google.com', 'tutorial'=>'', 'taobao'=>'taobao.com'); print"$data{'google'} = $data{'google'}n"; print"$data{'tutorial'} = $data{'tutorial'}n"; print"$data{'taobao'} = $data{'taobao'}n";
Executing the above program gives the following output:
!(#)
* * *
## Extracting Hash Values
You can extract values from a hash just like you would from an array.
The syntax for extracting hash values into an array is: **@{key1,key2}**.
## Example
#!/uer/bin/perl%data = (-taobao =>45, -google =>30, -tutorial =>40); @array = @data{-taobao, -tutorial}; print"Array : @arrayn";
Executing the above program gives the following output:
Array : 45 40
* * *
## Reading Hash Keys and Values
### Reading all keys
We can use the **keys** function to read all the keys of a hash. The syntax is as follows:
keys %HASH
This function returns an array of all the keys in the hash.
## Example
#!/usr/bin/perl %data = ('google'=>'google.com', 'tutorial'=>'', 'taobao'=>'taobao.com'); @names = keys%data; print"$namesn"; print"$namesn"; print"$namesn";
Executing the above program gives the following output:
taobao google tutorial
Similarly, we can use the **values** function to read all the values of a hash. The syntax is as follows:
values %HASH
This function returns an array of all the values in the hash.
## Example
#!/usr/bin/perl %data = ('google'=>'google.com', 'tutorial'=>'', 'taobao'=>'taobao.com'); @urls = values%data; print"$urlsn"; print"$urlsn"; print"$urlsn";
Executing the above program gives the following output:
taobao.com google.com
* * *
## Checking if an Element Exists
If you try to read a non-existent key/value pair from a hash, it will return an **undefined** value and generate a warning during execution.
To avoid this, we can use the **exists** function to check if a key exists before reading it:
## Example
#!/usr/bin/perl%data = ('google'=>'google.com', 'tutorial'=>'', 'taobao'=>'taobao.com'); if(exists($data{'facebook'})){print"facebook the URL is $data{'facebook'} n"; }else{print"facebook key does not existn"; }
Executing the above program gives the following output:
facebook key does not exist
In the code above, we used an **IF...ELSE** statement, which we will cover in detail in later chapters.
* * *
## Getting the Size of a Hash
The size of a hash is the number of elements it contains. We can get the size by first getting an array of all keys or values, and then counting the elements in that array. Here is an example:
## Example
#!/usr/bin/perl%data = ('google'=>'google.com', 'tutorial'=>'', 'taobao'=>'taobao.com'); @keys = keys%data; $size = @keys; print"1 - hash size: $sizen"; @values = values%data; $size = @values; print"2 - hash size: $sizen";
Executing the above program gives the following output:
1 - hash size: 32 - hash size: 3
* * *
## Adding or Deleting Elements in a Hash
Adding a key/value pair can be done with simple assignment. However, to delete a hash element, you need to use the **delete** function:
## Example
#!/usr/bin/perl%data = ('google'=>'google.com', 'tutorial'=>'', 'taobao'=>'taobao.com'); @keys = keys%data; $size = @keys; print"1 - hash size: $sizen"; $data{'facebook'} = 'facebook.com'; @keys = keys%data; $size = @keys; print"2 - hash size: $sizen"; delete$data{'taobao'}; @keys = keys%data; $size = @keys; print"3 - hash size: $sizen";
Executing the above program gives the following output:
1 - hash size: 32 - hash size: 43 - hash size: 3
* * *
## Iterating Over a Hash
We can iterate over a hash using foreach and while:
## Example - Using foreach
#!/usr/bin/perl%data = ('google'=>'google.com', 'tutorial'=>'', 'taobao'=>'taobao.com'); foreach$key(keys%data){print"$data{$key}n"; }
## Example - Using while
#!/usr/bin/perl%data = ('google'=>'google.com', 'tutorial'=>'', 'taobao'=>'taobao.com'); while(($key, $value) = each(%data)){print"$data{$key}n"; }
Executing the above program gives the following output:
google.com taobao.com
YouTip