Perl | Hashes - GeeksforGeeks

13 Apr, 2021
Set of key/value pair is called a Hash. Each key in a hash structure are unique and of type strings. The values associated with these keys are scalar. These values can either be a number, string or a reference. A Hash is declared using my keyword. The variable name is preceded by the dollar sign($)followed by key under curly braces and the value associated with the key. Each key is associated with a single value.Example: 
my%rateof{mango} = 45;
Question arises when to use Array and when to use Hashes? 
If the things are in order, then go for the array. For Example:  
1. A list of people in a bank queue.
2. A list of people in railway reservation line.
3. A list of files to read.
If the things we have are not in order then go for Hashes. For example: 
1. An index of surname looked up by the first name.
2. An index showing the size of files looked up by name.
Empty Hash: A hash variable without any key is called empty hash. 
Example: 
my %rateof;
Here rateof is the hash variable.
Inserting a key/value pair into a Hash: Keys are always of string type and values always of scalar type. 
Example: 
$rateof{'mango'} = 45; 
Here the key is mango and value is 45.
Initialization of Hash & Fetching an element of a Hash: A hash variable can be initialized with key/value pairs during its declaration time. There are two ways to initialize a hash variable. One is using => which is called the fat arrow or fat comma. The second one is to put the key/value pairs in double quotes(“”) separated by a comma(,). Using fat commas provide an alternative as you can leave double quotes around the key. To access the individual elements from a hash you can use a dollar sign($) followed by a hash variable followed by the key under curly braces. While accessing the key/value pair if you passed the key that doesn’t present in it then it will return an undefined value or if you switched on the warning it will show the warning. 
Example:  
Perl
# Perl program to demonstrate the# Fetching an element of a Hash # creating hash%rateof = ('Mango' => 45, 'Orange' => 30, 'Grapes' => 40); # Fetching an element of Hashprint "$rateof{'Mango'}\n";print "$rateof{'Orange'}\n";print "$rateof{'Grapes'}\n";
Output:  
45
30
40
Explanation: To access three keys Mango, Orange, Grapes and to print the values associated with it just use the dollar sign followed by Key. The print statement will print the value associated with that key.
Empty values in a Hash: Generally, you can’t assign empty values to the key of the hash. But in Perl, there is an alternative to provide empty values to Hashes. By using undef function. “undef” can be assigned to new or existing key based on the user’s need. Putting undef in double quotes will make it a string not an empty value. 
Perl
# Perl program to demonstrate the# empty values of a Hash #use warnings; # creating hash%rateof = ('Mango' => 45, 'Orange' => undef, 'Grapes' => 40); # Fetching an element of Hashprint "$rateof{'Mango'}\n";print "$rateof{'Orange'}\n";print "$rateof{'Grapes'}\n";
Output:  
45

40
Iterating over hashes: To access the value in a hash user must the know the key associate to that value. If the keys of a hash are not known prior then with the help of keys function, user can get the list of keys and can iterate over those keys.Example:  
my @fruits = keys %rateof;
for my $fruit (@fruits) {
    print "The color of '$fruit' is $rateofof{$fruit}\n";
}
Size of a hash: The number of key/value pairs is known as the size of hash. To get the size, the first user has to create an array of keys or values and then he can get the size of the array. 
Syntax:  
print scalar keys % hash_variable_name;
Example:  
Perl
# Perl program to find the size of a Hash #use warnings; # creating hash%rateof = ('Mango' => 64, 'Apple' => 54, 'Grapes' => 44, 'Strawberry'=>23); # creating array of keys@keys = keys %rateof;$size = @keys;print "Hash size using Keys is: $size\n"; # creating hash of values@values = values %rateof;$size = @values;print "Hash size using Values is: $size\n";
Output: 
Hash size using Keys is: 4
Hash size using Values is: 4
Adding and Removing Elements in Hashes:User can easily add a new pair of key/values into a hash using simple assignment operator.  
Example 1: Addition of element in hash  
Perl
# Perl program to add an element in a hash #use warnings; # creating hash%rateof = ('Mango' => 64, 'Apple' => 54, 'Grapes' => 44, 'Strawberry'=>23); # array of keys@keys = keys %rateof; $size = @keys;print "SIZE OF HASH BEFORE ADDING:  is $size\n"; # Adding new key/value pair into hash$rateof{'Potato'} = 20; # array of keys@keys= keys %rateof; $size = @keys;print "SIZE OF HASH AFTER ADDING:  is $size\n";
Output:  
SIZE OF HASH BEFORE ADDING:  is 4
SIZE OF HASH AFTER ADDING:  is 5
Example 2: Deleting an element from hash using delete function  
Perl
# Perl program to element from hash #use warnings; # creating hash%rateof = ('Mango' => 64, 'Apple' => 54, 'Grapes' => 44, 'Strawberry'=>23); # creating array of keys@keys= keys %rateof; # finding size of hash$size = @keys;print "SIZE OF HASH BEFORE DELETING: $size\n"; # using delete functiondelete $rateof{'Mango'}; # creating array of keys@keys= keys %rateof; # finding size of hash$size = @keys;print "SIZE OF HASH AFTER DELETING: $size\n";
Output:  
SIZE OF HASH BEFORE DELETING: 4
SIZE OF HASH AFTER DELETING: 3
 
Akanksha_Rai
shubham_singh
simranarora5sos
Perl-hashes
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Perl | Polymorphism in OOPs
Perl | Arrays
Perl | Arrays (push, pop, shift, unshift)
Perl | length() Function
Perl | sleep() Function
Perl | Data Types
Perl Tutorial - Learn Perl With Examples
Perl | Boolean Values
Introduction to Perl
Perl | Automatic String to Number Conversion or Casting