Perl hash examples tutorial

Perl Hash Examples



Creating a hash


First example is simple creating a hash in Perl. It shows two ways in which you can define the hash variable.


Perl Hash Example creating a hash in Perl



# Perl program for creating hash variable
my %monthHash = ("1","January","2","February");
my %monthHash1 = (
"1" => "January",
"2" => "February"
);


Printing Hash in Perl


In this example we are going to use two scalar variables key and value to call each key and value of the Hash monthHash. The each() function takes a monthHash. First it removes the topmost "Key and Value" pair by storing this into the variables $key and $value. So in the iteration it prints the key and value pair until monthHash finishes. Then each() returns false and while loop stops.

Perl Hash Example printing a hash in Perl



while (($key, $value) = each(%monthHash)){
print $key.", ".$value."\n";
}


Sorting Hash in Perl



Sorting hash using key in Perl and Sorting hash values in Perl
Following example shows how we can sort the Hash using key and values.

Perl Hash Example sorting a hash in Perl



# Perl program to sort hash variable

my %marksHash = (
"Robert" => 95,
"Alex" => 72,
"Brian" => 85,
"Jacob" => 77,
"Deren" => 99
);
print "Existing original array\n";
while (($key, $value) = each(%marksHash)){
print $key.", ".$value."\n";
}
print "Sorting hash keys\n";
foreach $key (sort keys %marksHash) {
print "$key: $marksHash{$key}\n";
}
print "Sorting hash values\n";

foreach $value (sort {$marksHash{$a} cmp $marksHash{$b} }
keys %marksHash)
{
print "$value $marksHash{$value}\n";
}

Output:
Existing original array
Brian, 85
Deren, 99
Alex, 72
Jacob, 77
Robert, 95
Sorting hash keys
Alex: 72
Brian: 85
Deren: 99
Jacob: 77
Robert: 95
Sorting hash values
Alex 72
Jacob 77
Brian 85
Robert 95
Deren 99




Adding and deleting elements from hash



Following program shows how to add and delete elements from the Hash i.e. perl delete key from hash

Perl Hash Example sorting a hash in Perl


 
# Perl program to add and delete element in hash variable


my %marksHash = (
"Robert" => 95,
"Alex" => 72,
"Brian" => 85,
"Jacob" => 77,
"Deren" => 99
);
print "Existing original array\n";
while (($key, $value) = each(%marksHash)){
print $key.", ".$value."\n";
}
# Adding new element pairs to Hash
$marksHash{"Steve"} = 88;
$marksHash{"Julian"} = 78;
print "After adding new elements\n";
while (($key, $value) = each(%marksHash)){
print $key.", ".$value."\n";
}




Output:
Existing original array
Brian, 85
Deren, 99
Alex, 72
Jacob, 77
Robert, 95
After adding new elements
Brian, 85
Julian, 78
Deren, 99
Alex, 72
Jacob, 77
Steve, 88
Robert, 95
After deleting elements
Julian, 78
Alex, 72
Jacob, 77
Steve, 88
Robert, 95

Thus we have seen Perl examples for creating a hash, printing a hash, sorting a hash, adding and deleting elements from hash.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment