Perl Hashes
Hashes are collections of key/value pairs.
In Perl, hash variables start with a percentage sign (%) symbol.
The format for accessing hash elements is: ${key}.
Below is a simple example of a hash:
Example
#!/usr/bin/perl
%data = ('google', 'google.com', 'tutorialpro', 'tutorialpro.org', 'taobao', 'taobao.com');
print "\$data{'google'} = $data{'google'}\n";
print "\$data{'tutorialpro'} = $data{'tutorialpro'}\n";
print "\$data{'taobao'} = $data{'taobao'}\n";
Executing the above program will output:
Creating Hashes
Hashes can be created using the following two methods:
1. Setting each key to a value
$data{'google'} = 'google.com';
$data{'tutorialpro'} = 'tutorialpro.org';
$data{'taobao'} = 'taobao.com';
2. Setting through a list
The first element in the list is the key, and the second is the value.
%data = ('google', 'google.com', 'tutorialpro', 'tutorialpro.org', 'taobao', 'taobao.com');
You can also use the => symbol to set key/value pairs:
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
The following example is a variation of the above, using - instead of quotes:
%data = (-google=>'google.com', -tutorialpro=>'tutorialpro.org', -taobao=>'taobao.com');
With this method, keys cannot contain spaces, and elements are accessed as:
$val = $data{-google};
$val = $data{-tutorialpro};
Accessing Hash Elements
The format for accessing hash elements is: ${key}. Here is an example:
Example
#!/usr/bin/perl
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
print "\$data{'google'} = $data{'google'}\n";
print "\$data{'tutorialpro'} = $data{'tutorialpro'}\n";
print "\$data{'taobao'} = $data{'taobao'}\n";
Executing the above program will output:
Extracting Hash Values
You can extract values from a hash just like from an array.
The syntax for extracting hash values into an array is: @{key1,key2}.
Example
#!/usr/bin/perl
%data = (-taobao => 45, -google => 30, -tutorialpro => 40);
@array = @data{-taobao, -tutorialpro};
print "Array : @array\n";
Executing the above program will output:
Array : 45 40
Reading Keys and Values from a Hash
Reading All Keys
You can use the keys function to read all keys from a hash. The syntax is:
keys %HASH
This function returns an array of all keys in the hash.
Example
#!/usr/bin/perl
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
@names = keys %data;
print "$names[0]\n";
print "$names[1]\n";
print "$names[2]\n";
Executing the above program will output:
taobao
google
tutorialpro
Similarly, you can use the values function to read all values from a hash. The syntax is:
values %HASH
This function returns an array of all values in the hash.
Example
#!/usr/bin/perl
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
@urls = values %data;
print "$urls[0]\n";
print "$urls[1]\n";
print "$urls[2]\n";
Executing the above program will output:
google.com
tutorialpro.org
taobao.com
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
@urls = values %data;
print "$urls[0]\n";
print "$urls[1]\n";
print "$urls[2]\n";
Executing the above program, the output is:
taobao.com
tutorialpro.org
google.com
Checking for Element Existence
If you read a non-existent key/value pair in a hash, it returns an undefined value and a warning is issued during execution.
To avoid this, you can use the exists function to check if a key exists before reading it:
Example
#!/usr/bin/perl
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
if( exists($data{'facebook'} ) ){
print "The URL for facebook is $data{'facebook'} \n";
}
else
{
print "The key 'facebook' does not exist\n";
}
Executing the above program, the output is:
The key 'facebook' does not exist
In the above code, we used the IF...ELSE statement, which will be introduced in later chapters.
Getting the Size of a Hash
The size of a hash is the number of elements it contains. You can get the size by first obtaining an array of all keys or values, then counting the elements in the array, as shown in the example below:
Example
#!/usr/bin/perl
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
@keys = keys %data;
$size = @keys;
print "1 - Hash size: $size\n";
@values = values %data;
$size = @values;
print "2 - Hash size: $size\n";
Executing the above program, the output is:
1 - Hash size: 3
2 - Hash size: 3
Adding or Removing Elements in a Hash
Adding a key/value pair can be done by simple assignment. However, to remove an element from a hash, you need to use the delete function:
Example
#!/usr/bin/perl
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
@keys = keys %data;
$size = @keys;
print "1 - Hash size: $size\n";
# Adding an element
$data{'facebook'} = 'facebook.com';
@keys = keys %data;
$size = @keys;
print "2 - Hash size: $size\n";
# Deleting an element from the hash
delete $data{'taobao'};
@keys = keys %data;
$size = @keys;
print "3 - Hash size: $size\n";
Executing the above program, the output is:
1 - Hash size: 3
2 - Hash size: 4
3 - Hash size: 3
Iterating Over a Hash
You can iterate over a hash using foreach and while:
Example - Using foreach
#!/usr/bin/perl
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
foreach $key (keys %data){
print "$data{$key}\n";
}
Example - Using while
#!/usr/bin/perl
%data = ('google'=>'google.com', 'tutorialpro'=>'tutorialpro.org', 'taobao'=>'taobao.com');
while(($key, $value) = each(%data)){
print "$data{$key}\n";
}
Executing the above program, the output is:
tutorialpro.org
google.com
taobao.com