Updated April 12, 2023
Definition of Perl Hash
In Perl, the hash is defined as an associative array consisting of an unordered collection of key-value pairs having the key with its unique string and values are scalar and the hashes are also considered as a data structure similar to arrays, dictionaries, etc in Perl. In general, the hash in Perl is defined as a collection of items or elements which consists of an unordered key-value pair where the values can be accessed by using the keys specified to each value, and in Perl, hash variables are denoted or preceded by a percent (%) symbol and a single element can be referred by using “$” symbol followed by key and value in the curly braces.
Functions of Hashes in Perl
In this article, we will discuss hash in Perl which is defined as a data structure that has the collection or set of elements having key-value pair and each value that can be of any type such as string, number, or a reference, and these values are accessed by their respective key where keys are unique and are of string type in the entire hash which has no repeating keys. If there are no keys in the hash then they are considered as an empty hash. Therefore we can use hash whenever any data are not in order. Note that if we want to access a particular value then we should know the appropriate key of that value and if the key is not known then we need to use the key function to access the hash to get the entire list of keys and then we can iterate over the keys to find the particular value.
Now let us see syntax and examples for hash in Perl programming language usually few developers use “my” keyword for declaring any variables in Perl and to declare key this keyword “%key_name”. Here we will see without using the “my” keyword.
Syntax:
There are two ways where we can declare hashes using key-value pairs:
-
$key{'key_name'} = key_value;
-
%key = ('key_name' => key_value);
In the above, there are two ways of defining or declaring hash in Perl. In the first syntax, we can see we have assigned a value to keys that are named and will be placed within the curly braces and this can be used when we want to declare each key-value pair one by one and the key is preceded by “$”.
In the second syntax, we are declaring the list of the key-value pair by using key preceded with %, and to make it easy each key is followed by => to point to its values in the list of the key-value pair. And if using key reference by values then we need to use the format as “$value = %key{-key_name}” note we need to use “-” before key_name.
Now let use the demonstration of using hashes in Perl in the below examples:
Examples
Let us discuss examples of Perl Hash
Example #1:
In the below example we will simple program for creating a hash, initializing hash, and accessing the hash in Perl.
Code:
#!/usr/bin/perl
print "Demonstration of creation or initialization and accessing of the hash in Perl";
print "\n";
print "\n";
print "The first hash declaration as a list of key value pair";
%institute = ('Educba' => 1, 'Google' => 2, 'Firefox' => 3);
print "\n";
foreach $key (keys %institute)
{
print "$key $institute{$key}\n";
}
print "\n";
print "The second hash declaration with one by one key and value pair:";
$course{'Python'} = 59;
$course{'Java'} = 10;
$course{'Perl'} = 68;
print "\n";
foreach $key (keys %course)
{
print "$key $course{$key}\n";
}
print "\n";
print "Accessing of each element in the hash in the above two hashes are: ";
print "\n";
print "$institute{'Educba'}\n";
print "$course{'Python'}\n";
print "$course{'Perl'}\n";
Output:
In the above program, we can see have declared two hashes in two different ways as shown in the above syntax section. In the above code, we have created hash using the same format as in the above syntax given then we are printing both the hash elements using “foreach” loop which will traverse the entire hash and prints both key and value pair where we can also use “while each “ loop if in case the hashes are very large. Then we are accessing or fetching each element in the two hashes using its key and the key name which is placed within the curly braces to access the values of keys.
Example #2:
Now we will see another example of how to add and remove elements in hash and how to sort the hash in Perl.
Code:
#!/usr/bin/perl
print "Demonstration of addition and deletion of each elements of hash in Perl";
print "\n";
print "\n";
print "The first hash declaration ";
%institute = ('Educba' => 1, 'Google' => 2, 'Firefox' => 3);
print "\n";
@keys = keys %institute;
$institute{'Opera'} = 4;
@keys= keys %institute;
foreach $key (keys %institute)
{
print "$key $institute{$key}\n";
}
$size = @keys;
print "Size of the new hash after adding element is: $size\n";
print "\n";
print "The second hash declaration :";
$course{'Python'} = 59;
$course{'Java'} = 10;
$course{'Perl'} = 68;
print "\n";
@keys= keys %course;
delete $course{'Java'};
@keys= keys %course;
foreach $key (keys %course)
{
print "$key $course{$key}\n";
}
$size = @keys;
print "Size of the new hash after deleting element is : $size\n";
print "\n";
print "The third hash declaration";
%employee = (
"Ram" => "Uttar pradesh",
"Sam" => "Goa",
"Krish" => "Karanataka",
"Atul" => "Kerla"
);
print "\n";
foreach $key (keys %employee)
{
print "$key $employee{$key}\n";
}
print "\n";
print "After sorting the third hash using sort keyword";
print "\n";
foreach $key (sort keys %employee) {
print "$key: $employee{$key}\n";
}
Output:
In the above program, we can see we have declared 3 hashes one to show the addition of element by just using the syntax of the creation of a single element in hash and printing the size of the hash after adding an element using the “size” keyword. Then in the second hash, we are deleting an element and printing the hash and its size using keywords “delete” for deleting the elements. Then in the third hash, we are sorting the hash using the “sort” keyword”. The output can be seen in the above screenshot.
Conclusion
In this article, we conclude that hash in Perl is also like other data structures used on unordered key-value pair elements. In this article, we saw how to create or initialize hash along with accessing the values of the keys and printing the entire hash using the “foreach” loop with a simple example. Then we also saw an example showing how to add and remove elements and also sort elements using “sort” and finding the size of the hash using “size”.
Recommended Articles
This is a guide to Perl Hash. Here we also discuss the Definition and Working of Hashes in Perl along with different examples and its code implementation. You may also have a look at the following articles to learn more –