Updated April 11, 2023
Definition of Perl print hash
The Perl print hash is printing the hash type of data in the Perl technology using methods and conditions. It is useful to display the key and value set of the data in the Perl language. It is displayed the arbitrary size or fixed size of data in the Perl hash function. It helps to set the user’s data in the Perl hash using keys and values and display it on the output screen. It is necessary for printing Perl hash function elements using Perl loop and condition statements.
Syntax:
The basic Perl print hash syntax is below.
- First way
$ perl_print_hash_variable{'hash_key1'} = 'hash value in string format';
$ perl_print_hash_variable{'hash_key2'} = 141;
print "$ perl_print_hash_variable{'hash_key1'} \n";
print "$ perl_print_hash_variable{'hash_key2'} \n";
- Second way
% perl_print_hash_variable = ('hash_key1', 'hash value in string format', 'hash_key2', 141,);
print "$ perl_print_hash_variable{'hash_key1'} \n";
print "$ perl_print_hash_variable{'hash_key2'} \n";
- Third way
% perl_print_hash_variable = ('hash_key1' => 'hash value in string format', 'hash_key2' => 141,);
print "$ perl_print_hash_variable{'hash_key1'} \n";
- Fourth way
% perl_print_hash_variable = (-hash_key1 => 'hash value in string format', -hash_key2 => 141);
print "$ perl_print_hash_variable{'-hash_key1'} \n";
print "$ perl_print_hash_variable{'-hash_key2'} \n";
Description:
It can use $ symbol for a single hash key and its value. It can use the % symbol for multiple hash keys and their values.
With “foreach” loop syntax is below.
% perl_print_hash_variable = ('hash_key1' => 'hash value in string format', 'hash_key2' => 141);
foreach $key (keys %perl_print_hash_variable)
{
print "$key : $perl_print_hash_variable{$key} \n";
}
Description:
The foreach loop is using to print multiple Perl hash elements. Users can apply the print keyword once and display all hash keys and values. The foreach loop print unordered hash key and value.
- With “while/each” loop syntax is below.
% perl_print_hash_variable = ('hash_key1' => 'hash value in string format', 'hash_key2' => 141);
while (($key, $value) = each (%perl_print_hash_variable))
{
print "$key : $perl_print_hash_variable{$key} \n";
}
Description:
The while/each loop is using for print multiple Perl hash elements. Users can apply the print keyword once and display all hash keys and values. The while/each loop print ordered hash key and value.
How to print hash in Perl?
- The download the Perl software and install it in your operating system of the device.
https://www.Perl.org/ or http://strawberryPerl.com/ are commonly using Perl software websites.
- Make a file with the Perl extension in the device and save the file in the command line path.
Example:
helloo.pl or first pearl.pl
- Create the Perl hash variable and Initialize with Perl key and value.
% perl_print_hash_variable = ('hash_key1' => 'hash value in string format', 'hash_key2' => 141);
- Syntax used to display Perl hash single element.
print "$ perl_print_hash_variable{'hash_key1'} \n;
- Syntax is using to display Perl hash multiple key and value.
foreach $key (keys %perl_print_hash_variable)
{
print "$key : $perl_print_hash_variable{$key} \n";
}
- Combine the working step
% perl_print_hash_variable = ('Perl' =>'backend', 'Bootstrap' =>'frontend', 'JavaScript' => 'frontend and backend ', 'MySql' =>'Database', 'Angular' =>'Web Design');
foreach $key (keys %perl_print_hash_variable)
{
print "$key : $perl_print_hash_variable{$key} \n";
}
Examples
Below are the different examples mentioned:
Example #1
Code:
$ perl_print_hash_variable{'Name'} = 'Perl technology';
$ perl_print_hash_variable{'Number'} = 141;
$ perl_print_hash_variable{'complex'} = 'Perl @ 141';
print "First way to print hash function in Perl language. \n";
print "$ perl_print_hash_variable{'Name'} \n";
print "$ perl_print_hash_variable{'Number'} \n";
print "$ perl_print_hash_variable{'complex'} \n";
% perl_print_hash_variable1 = ('Name', 'Perl technology', 'Number', 141, 'complex','Perl @ 141');
print "Second way to print hash function in Perl language. \n";
print "$ perl_print_hash_variable{'complex'} \n";
% perl_print_hash_variable2 = ('Name' => 'Perl technology', 'Number' => 141, 'complex' => 'Perl @ 141');
print "Third way to print hash function in Perl language. \n";
print "$ perl_print_hash_variable2{'Name'} \n";
print "$ perl_print_hash_variable2{'Number'} \n";
% perl_print_hash_variable3 = (-Name => 'Perl technology', -Number => 141, -complex => 'Perl @ 141');
print "fourth way to print hash function in Perl language. \n";
print "$ perl_print_hash_variable3{'-Name'} \n";
print "$ perl_print_hash_variable3{'-complex'} \n";
Output:
Example #2 – With foreach loop
Code:
$ perl_print_hash_variable{'Name'} = 'Perl technology';
$ perl_print_hash_variable{'Number'} = 141;
$ perl_print_hash_variable{'complex'} = 'Perl @ 141';
print "Perl print hash with foreach loop key and value is below. \n";
foreach $key (keys %perl_print_hash_variable)
{
print "$key : $perl_print_hash_variable{$key} \n";
}
% perl_print_hash_variable2 = ('Anna' => 140, 'merry' => 141, 'smith' => 142, 'zineth' => 143);
print "Perl print hash with foreach loop key and value is below. \n";
foreach $key (keys %perl_print_hash_variable2)
{
print "$key : $perl_print_hash_variable2{$key} \n";
}
% perl_print_hash_variable3 = ('Perl' =>'backend', 'Bootstrap' =>'frontend', 'JavaScript' =>'frontend and backend ', 'MySql' =>'Database', 'Angular' =>'Web Design');
print "Perl print hash with foreach loop key and value is below. \n";
foreach $key (keys %perl_print_hash_variable3)
{
print "$key : $perl_print_hash_variable3{$key} \n";
}
Output:
Example #3 – With while/each loop
Code:
$ perl_print_hash_variable{'Name'} = 'Perl technology';
$ perl_print_hash_variable{'Number'} = 141;
$ perl_print_hash_variable{'complex'} = 'Perl @ 141';
print "Perl print hash with while/each loop key and value is below. \n";
while (($key, $value) = each (%perl_print_hash_variable))
{
print "$key : $perl_print_hash_variable{$key} \n";
}
% perl_print_hash_variable2 = ('Anna' => 140, 'merry' => 141, 'smith' => 142, 'zineth' => 143);
print "Perl print hash with while/each loop key and value is below. \n";
while (($key, $value) = each (%perl_print_hash_variable2))
{
print "$key : $perl_print_hash_variable2{$key} \n";
}
% perl_print_hash_variable3 = ('Perl' =>'backend', 'Bootstrap' =>'frontend', 'JavaScript' =>'frontend and backend ', 'MySql' =>'Database', 'Angular' =>'Web Design');
print "Perl print hash with while/each loop key and value is below. \n";
while (($key, $value) = each (%perl_print_hash_variable3))
{
print "$key : $perl_print_hash_variable3{$key} \n";
}
Output:
Conclusion
- It is use to display multiple keys and their value as per requirement.
- It ordered and unordered hash key and value as per the user’s choice.
- It is sorted, easy to use, and user friendly function in Perl technology.
Recommended Articles
We hope that this EDUCBA information on “Perl print hash” was beneficial to you. You can view EDUCBA’s recommended articles for more information,