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 a 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:

  • The value is directly assigned as shown below and data is added to existing hash.
    $stud{'Comp'} = 45;
    $stud{'Inft'} = 42;
    $stud{'Extc'} = 35;
    
  • Another way is by using list which gets converted to hash by taking individual pairs. The first element of the pair is used as the key, and the second is taken as the value.
    %stud = ('Comp', 45, 'Inft', 42, 'Extc', 35);
  • One way is using => to indicate the key/value pairs as shown below :
    %stud = ('Comp' => 45, 'Inft' => 42, 'Extc' => 35);
  • Another way of doing the same thing is as shown below. Here all the keys are preceded by hyphen (-) and so no quotation is required around them. Hash can be build using this method only if there are no spaces in the word.

    %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.
Below Example illustrates all of the methods of Hash creation as explained above:
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Initializing Hash1 by 
# directly assinging 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 
# indiviual 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'}";

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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";

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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";

chevron_right


Output:

Values are :
45
35
42


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.