Skip to main content

Perl Hash

Hash is combination of key and value pair. Their element are stored unordered sequence. In perl programming hash variable are declare by % symbol.

#!/usr/bin/perl
use warnings;
use strict;


our %record = (computer=>100,
	      laptop =>200,
	      keyword=>300,
	      mouse=>100);

print $record{'computer'},"\n"; #100
print %record{computer},"\n"; #key and pair
100
computer100
Perl Hash Example

hash is contain key and associative values. keys() methods are returning an array which is include all keys. And values() methods are return an array which is contain all values.

#!/usr/bin/perl
use warnings;
use strict;


our %record = (computer=>100,
	      laptop =>200,
	      keyword=>300,
	      mouse=>100);

our @all_keys = keys(%record);
our @all_values = values(%record);

#display keys
print $all_keys[0],"\n";
print $all_keys[1],"\n";
print $all_keys[2],"\n";
print $all_keys[3],"\n";

print "-------------\n";
#display values
print $all_values[0],"\n";
print $all_values[1],"\n";
print $all_values[2],"\n";
print $all_values[3],"\n";
Get keys and values of hash

Hash Operations

There are the main three operation are have which are modified the current state of an hash such as insert new element, updating existing element value and deleting the exist element etc.

Add Hash element

#!/usr/bin/perl
use warnings;
use strict;


our %record = (computer=>100,
	      laptop =>200,
	      keyword=>300,
	      mouse=>100);
print "Before Add \n";
while (($key, $value) = each (%record))
{
	print $key,"=>",$value,"\n";
}

#add new record
$record{"webcam"}=400;

print "\n\nAfter Add \n";
print "-------------\n";

while (($key, $value) = each (%record))
{
	print $key,"=>",$value,"\n";
}
Add New Hash Element
Before Add 
mouse=>100
laptop=>200
computer=>100
keyword=>300


After Add 
-------------
laptop=>200
computer=>100
webcam=>400
mouse=>100
keyword=>300

Update Hash Element

#!/usr/bin/perl
package Regular;
use warnings;
use strict;


our %record = (computer=>10,
	      laptop =>20,
	      keyword=>30,
	      mouse=>5);
print "Before update Hash \n";
while (my( $key, $value) = each (%record))
{
	print $key,"=>",$value,"\n";
}

$record{"laptop"}=900;
$record{"computer"}=800;

print "\n\nAfter update  Hash\n";
print "-------------\n";

while (my ($key, $value) = each (%record))
{
	print $key,"=>",$value,"\n";
}
Perl Update Hash Element
Before update Hash 
keyword=>30
laptop=>20
mouse=>5
computer=>10


After update  Hash
-------------
keyword=>30
laptop=>900
mouse=>5
computer=>800

Delete Hash Element

#!/usr/bin/perl
package Regular;
use warnings;
use strict;


our %color = (
	green=>"#008000",
	orange=>"#ffa500",
	yellow=>"#ffff00",
	skyblue=>"#87ceeb",
	black =>"#000000"
);
print "Before delete  \n";
while (my ($key, $value) = each (%color))
{
	print $key,"=>",$value,"\n";
}
delete($color{'black'});
print "\n\nAfter Delete  black \n";
print "-------------\n";

while (my ($key, $value) = each (%color))
{
	print $key,"=>",$value,"\n";
}
Delete hash element in perl programming
Before delete  
green=>#008000
orange=>#ffa500
skyblue=>#87ceeb
black=>#000000
yellow=>#ffff00

After Delete  black 
-------------
green=>#008000
orange => #ffa500 
skyblue => #87ceeb 
yellow => #ffff00 




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment