Syntax:
Different syntaxes for declaring hash in Perl are shown in the following:
Or
%hash_variable(Key1, Value1, Key2, Value2,………KeyN, ValueN)
Or
%hash_variable = (
Key1 => Value1,
Key2 => Value2,
………
KeyN => ValueN
)
Different Examples of Hash
The ways of declaring the hash variables, accessing single and multiple hash values, inserting data into the hash variable, and searching a value in the hash variable are shown in this part of the tutorial.
Example 1: Print the Hash Value Based on the Key
Create a Perl file with the following script that prints the particular value of the hash variable based on the key value. The hash variable of five elements are declared in the script. Next, a string value is taken from the user as a key value of the hash variable. If the valid key value is taken from the user, the corresponding hash value is printed.
use warnings;
use strict;
use Data::Dumper;
#Declare a hash variable
my %marks = ('Mir Sabbir' => 78, 'Nila Hossain' => 84, 'Ahmmed Ali' => 90, 'Abir Chowdhury' => 55, 'Zinia Ali' => 95);
print Dumper(\%marks);
print "\nEnter the student name: ";
#Take the student name from the user
my $student = <>;
#Remove the newline character from the input value
chomp($student);
#Print the particular value of the hash key
print "$student got $marks{$student} marks.\n";
Output:
The following output appears if the input value is “Nila Hossain”:
Example 2: Print All Keys and Values of the Hash
Create a Perl file with the following script that prints all keys and values of the hash variable using the “while” loop. The key and value of each element of the hash variable are stored in two variables while iterating the loop and printing these values in the output.
use warnings;
use strict;
#Declare a hash variable
my %marks = ('Mir Sabbir' => 78, 'Nila Hossain' => 84, 'Ahmmed Ali' => 90, 'Abir Chowdhury' => 55, 'Zinia Ali' => 95);
print "The keys and values of the hash variable:\n";
#Iterate the hash values and print the keys and values
while ((my $k, my $v) = each (%marks))
{
print "$k : $marks{$k} \n";
}
Output:
The following output appears after executing the script:
Example 3: Adding a New Element into the Hash
Create a Perl file with the following script that inserts a new element in the previously declared hash variable. All elements of the hash variable are printed before and after adding the new element.
use warnings;
use strict;
#Declare a hash variable
my %marks = ('Mir Sabbir' => 78, 'Nila Hossain' => 84, 'Ahmmed Ali' => 90, 'Abir Chowdhury' => 55, 'Zinia Ali' => 95);
my $mark;
print "The content of the hash variable:\n";
foreach my $name (keys %marks)
{
#Read the key value
$mark = $marks{$name};
#Print the key and the corresponding value of the key
print " $name => $mark\n";
}
#Add a new value
$marks{'Samia Jahan'} = 75;
print "The content of the hash variable after addition:\n";
foreach my $name (keys %marks)
{
#Read the key value
$mark = $marks{$name};
#Print the key and the corresponding value of the key
print " $name => $mark\n";
}
Output:
The following output appears after executing the script:
Example 4: Search the Particular Value in the Hash
Create a Perl file with the following script that searches for a hash value based on the key value that is taken from the user using the exists() function:
use warnings;
use strict;
use Data::Dumper;
#Declare a hash variable
my %marks = ('Mir Sabbir' => 78, 'Nila Hossain' => 84, 'Ahmmed Ali' => 90, 'Abir Chowdhury' => 55, 'Zinia Ali' => 95);
print Dumper(\%marks);
print "\nEnter the student name: ";
#Take the student name from the user
my $student = <>;
#Remove the newline character from the input value
chomp($student);
#Search the particular value of the hash key
if (exists($marks{$student}))
{
print "$student got $marks{$student} marks.\n";
}
else
{
print "Student name not found.\n";
}
Output:
The following output appears after executing the script with the “Abir Chowdhury” value:
The following output appears after executing the script with the “Nirob Hasan” value:
Conclusion
The ways of using a hash variable in different ways are shown in this tutorial using multiple examples to help the new Perl users.