Hashing is the process of converting a given key into another value. A hash function is used to generate the new value (called hash) according to a mathematical algorithm.
A Perl hash is defined by key-value pairs. Perl stores elements of a hash in such an optimal way that you can look up its values based on keys very fast. Like a scalar or an array variable, a hash variable has its own prefix. A hash variable must begin with a percent sign (%). A hash key must be unique. If you try to add a new key-value pair with the key that already exists, the value of the existing key will be over-written.
Example:
#!/usr/bin/perl # defines a hash named subjects # the first scalar inside the # brackets are hashes and the # second one beside them are values my %subjects = qw(Physics Laws
Chemistry Exceptions
Mathematics Formulas
Programming Fun) ;
# getting the value of Programming print ( $subjects { 'Programming' });
|
Fun
To make code more elegant and easier to read, Perl provides the => operator. It helps differentiate between keys and values. The keys always points to their corresponding values with the => operator. However, here Perl requires the keys of a hash to be strings, meanwhile, the values can be any scalars. If you use non-string values as the keys, you may get an unexpected result. The $subjects hash can be rewritten using => operator as follows:
#!/usr/bin/perl # declaring hash using the => operator my %subjects = ( Physics => 'Laws' ,
Chemistry => 'Exceptions' ,
Mathematics => 'Formulas' ,
Programming => 'Fun' );
# getting the value of Mathematics print ( $subjects { 'Mathematics' });
|
Formulas
Odd number of elements in the hash assignment
When the number of elements in the hash assignment is odd, Perl gives us a warning as it notices that the number of elements being assigned to the hash cannot be divided by 2. That the number of elements is odd.
Since this is just a warning (and it is only displayed if ‘use warnings’ is in effect), the script will go on with the assignment. The odd elements (“Physics”, “Chemistry”, “Mathematics” and “Programming”) will become keys and the even elements (“Laws”, “Exceptions” and “Formulas”) will become the values. Because the number of elements is odd, the last key (“Programming”) will not have a pair.
#!/usr/bin/perl use warnings;
use strict;
# declaring hash with odd number # of assignments my %subjects = qw( Physics Laws
Chemistry Exceptions
Mathematics Formulas
Programming ) ;
print "Physics = $subjects{'Physics'}\n" ;
print "Chemistry = $subjects{'Chemistry'}\n" ;
print "Mathematics = $subjects{'Mathematics'}\n" ;
print "Programming = $subjects{'Programming'}\n" ;
|
Output
Physics = Laws Chemistry = Exceptions Mathematics = Formulas Programming = Odd number of elements in hash assignment at line 7. Use of uninitialized value $subjects{"Programming"} in print at line 12.
Adding, Removing and Modifying elements of a Hash
Adding new elements to the Hash and modifying them, are very similar to each other. The name of the hash is followed by the key enclosed within curly braces and assigned a value. We can use the delete function to remove a hash. Following sample code shows how to assign/modify and remove elements from a Hash:
#!/usr/bin/perl # declaring a hash my %subjects = ( Physics => 'Laws' ,
Chemistry => 'Exceptions' ,
Mathematics => 'Formulas' ,
Programming => 'Fun' );
# extracting keys of hash in an array @keys = keys %subjects ;
# determining size of keys-array i.e. # number of elements in hash $size = @keys ;
print "Hash size is $size\n" ;
# adding an element to the hash; $subjects { 'Biology' } = 'Boring' ;
# determining size of hash after # adding an element @keys = keys %subjects ;
$size = @keys ;
print "Hash size is $size\n" ;
# removing an element from hash delete $subjects { 'Chemistry' };
# determining size of hash after # removing an element @keys = keys %subjects ;
$size = @keys ;
print "Hash size is $size\n" ;
# modifying an element of hash $subjects { 'Mathematics' } = 'Intriguing' ;
# looping over the hash for ( keys %subjects ){
print ( "$_ is $subjects{$_}\n" );
} |
Hash size is 4 Hash size is 5 Hash size is 4 Mathematics is Intriguing Physics is Laws Programming is Fun Biology is Boring