Perl

Perl Hash Examples

Many programming languages support associative array that contains key-value pair to store multiple data. Each key of this array is unique, each key can be a string or number, and the key values are not sorted like a numeric array. This type of data structure can be used in the Perl script which is called hash. The Perl hash contains a unique key that contains any scalar value such as a string or number. The “%” symbol is used to declare a hash variable in Perl. The ways of declaring and using the hash variable in Perl are shown in this tutorial.

Syntax:

Different syntaxes for declaring hash in Perl are shown in the following:

%hash_variable

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.

#!/usr/bin/perl

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

p1

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.

#!/usr/bin/perl

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:

p2

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.

#!/usr/bin/perl

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:

p3

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:

#!/usr/bin/perl

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:

p4-1

The following output appears after executing the script with the “Nirob Hasan” value:


p4-2

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.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.