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:
my %subjects = qw(Physics Laws
Chemistry Exceptions
Mathematics Formulas
Programming Fun) ;
print ( $subjects { 'Programming' });
|
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:
my %subjects = ( Physics => 'Laws' ,
Chemistry => 'Exceptions' ,
Mathematics => 'Formulas' ,
Programming => 'Fun' );
print ( $subjects { 'Mathematics' });
|
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.
use warnings;
use strict;
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:
my %subjects = ( Physics => 'Laws' ,
Chemistry => 'Exceptions' ,
Mathematics => 'Formulas' ,
Programming => 'Fun' );
@keys = keys %subjects ;
$size = @keys ;
print "Hash size is $size\n" ;
$subjects { 'Biology' } = 'Boring' ;
@keys = keys %subjects ;
$size = @keys ;
print "Hash size is $size\n" ;
delete $subjects { 'Chemistry' };
@keys = keys %subjects ;
$size = @keys ;
print "Hash size is $size\n" ;
$subjects { 'Mathematics' } = 'Intriguing' ;
for ( keys %subjects ){
print ( "$_ is $subjects{$_}\n" );
}
|
Output:
Hash size is 4
Hash size is 5
Hash size is 4
Mathematics is Intriguing
Physics is Laws
Programming is Fun
Biology is Boring
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 Jul, 2020
Like Article
Save Article