Open In App

Perl Hash

A hash is a set of key-value pairs. Perl stores elements of a hash such that it searches for the values based on its keys. Hash variables start with a ‘%’ sign. 

Perl requires the keys of a hash to be strings, whereas the values can be any scalars. These values can either be a number, string or reference. If non-string values are used as the keys, it gives an unexpected result. 



A hash key must be unique. If a new key-value pair is added to a hash and that key is existing then its corresponding value is overwritten. 

Creating Hashes

There are many ways to initialize a hash variable which are shown below:



$stud{'Comp'} = 45;
$stud{'Inft'} = 42;
$stud{'Extc'} = 35;
%stud = ('Comp', 45, 'Inft', 42, 'Extc', 35);
%stud = ('Comp' => 45, 'Inft' => 42, 'Extc' => 35);
%stud = (-Comp => 45, -Inft => 42, -Extc => 35);
Accessing Hash Elements

To access the individual element from the hash, variable is prefixed with a dollar sign ($) and then append the element key within curly braces after the name of the variable. 

The below Example illustrates all of the methods of Hash creation as explained above: 

Example :  




#!/usr/bin/perl
 
# Initializing Hash1 by
# directly assigning values
$stud1{'Comp'} = 10;
$stud1{'Inft'} = 20;
$stud1{'Extc'} = 30;
 
# printing elements of stud
print "Printing values of Hash1\n";
print "$stud1{'Comp'}\n";
print "$stud1{'Inft'}\n";
print "$stud1{'Extc'}\n";
 
# Initializing Hash2 by taking
# individual pairs
%stud2 = ('Comp', 15, 'Inft', 18, 'Extc', 22);
 
# Extracting values using keys
print "\nPrinting values of Hash2\n";
print "computer = $stud2{'Comp'}";
print "\ninft = $stud2{Inft}";
print "\nextc = $stud2{'Extc'}\n";
 
 
# Initializing Hash3 using '=>'
%stud3 = ('Comp' => 45, 'Inft' => 42, 'Extc' => 35);
 
# printing elements of stud3
print "\nPrinting values of Hash3\n";
print "$stud3{'Comp'}\n";
print "$stud3{'Inft'}\n";
print "$stud3{'Extc'}\n";
 
# Initializing Hash4 using hyphen(-)
%stud4 = (-Comp => 5, -Inft => 15, -Extc => 25);
 
# Printing elements of stud4
print "\nPrinting values of Hash4\n";
print "$stud4{'-Comp'}\n";
print "$stud4{'-Inft'}\n";
print "$stud4{'-Extc'}";

Output: 
Printing values of Hash1
10
20
30

Printing values of Hash2
computer = 15
inft = 18
extc = 22

Printing values of Hash3
45
42
35

Printing values of Hash4
5
15
25

 

Extracting Keys and Values

Sometimes there is a need to extract keys and values of a hash to perform various operations on it. Operations like element modification, deletion, addition, etc. Hash allows to extract these keys and values with the use of inbuilt functions. 
Getting a list of all of the keys from a hash can be done using keys function.

Syntax: keys %HASH 
Returns an array of all the keys present in the HASH

Example:  




# Initializing Hash with Key-Value pairs
%stud = ('Comp' => 45, 'Inft' => 42, 'Extc' => 35);
 
# Extracting keys from hash
@Key_array = keys %stud;
 
# Printing the extracted keys
print "Keys are :\n";
print "$Key_array[0]\n";
print "$Key_array[1]\n";
print "$Key_array[2]\n";

Output: 

Keys are :
Comp
Extc
Inft

Similarly, values function is used to get a list of all the values.  

Syntax: values %HASH 
Returns an array with all the values of HASH

Example:  




# Initializing Hash with Key-Value pairs
%stud = ('Comp' => 45, 'Inft' => 42, 'Extc' => 35);
 
# Extracting values from hash
@value_array = values %stud;
 
# Printing the extracted values
print "Values are :\n";
print "$value_array[0]\n";
print "$value_array[1]\n";
print "$value_array[2]\n";

Output: 
Values are :
45
35
42

 


Article Tags :