About Me

Friday 20 April 2012

PERL - Hashes

PERL - Hashes

Hashes are complex list data, like arrays except they link a key to a value. To define a hash, we use the percent (%) symbol before the name.

defineahash.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";

# DEFINE A HASH
%coins = ("Quarter", 25, "Dime", 10, "Nickel", 5);

# PRINT THE HASH
print %coins;

Display:

Nickel5Dime10Quarter25

PERL - Hash Indexing

Hashes can be indexed using two scalar variables. The variables $key and $value can be used to call on each key or value of the hash. We can use these variables to print out our hash again but this time let's make it a little more legible using a while loop.

legiblehash.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";

# DEFINE A HASH
%coins = ( "Quarter" , 25,
           "Dime" ,    10,
           "Nickel",    5 );
           
# LOOP THROUGH IT
while (($key, $value) = each(%coins)){
     print $key.", ".$value."<br />";
}

legiblehash.pl:

Nickel, 5
Dime, 10
Quarter, 25
The each() function takes a hash. It then removes the topmost "Key and Value" pair. We store this into the variables $key and $value. Each time this loop iterates, the statement ($key, $value) = each(%thing) executes and the hash pops off the top key value pair and will continue doing so until it has gone through every pair in the hash. When it is done, each() returns false and the loop stops running.
Hashes work really well with HTML Tables.

tablehashes.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";

# DEFINE A HASH
%coins = ( "Quarter" , 25,
           "Dime" ,    10,
           "Nickel",    5 );

# SET UP THE TABLE
print "<table border='1'>";
print "<th>Keys</th><th>Values</th>";

# EXECUTE THE WHILE LOOP
while (($key, $value) = each(%coins)){
     print "<tr><td>".$key."</td>";
     print "<td>".$value."</td></tr>";
}
print "</table>";

tablehashes.pl:

KeysValues
Nickel5
Dime10
Quarter25
We have yet to sort our hash so you may experience different arrangements of your keys than shown in the display box.

PERL - Sorting Hashes by Key

Sorting any hash requires the use of the sort() function that has previously been outlined in PERL Arrays. We must also specify to PERL that we plan on sorting our hash by the key or value elements. The example below illustrates how to sort a hash and then print back the resulting hash.

sortkeys.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";

# DEFINE A HASH
%coins = ( "Quarter" , 25,
           "Dime" ,    10,
           "Nickel",    5 );
# FOREACH LOOP
foreach $key (sort keys %coins) {
     print "$key: $coins{$key}<br />";
}

sortkeys.pl:

Dime: 10
Nickel: 5
Quarter: 25
The $coins($key) represents the variable assigned by PERL to each value of each element. Use this short cut to quickly retrieve each value element from your hashes.

PERL - Sorting Hashes by Value

Hashes may be sorted by value. Often times this can be a very tricky process especially if we are dealing with numbers. With our current example, we have a "5" value for our nickel element. This will cause some problems as if we just sort the hash by value as it is, the nickel value is a "5" instead of "05".
We need to edit our hash to and change is the values to floating-point numbers. This way PERL will properly sort our hash by our values as the nickel element will be valued according to a value of "05" instead of "5".

sortvalues.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";

# DEFINE A HASH
%coins = ( "Quarter" , .25,
           "Dime" , .10,
           "Nickel", .05 );
# FOREACH LOOP
foreach $value (sort {$coins{$a} cmp $coins{$b} }
           keys %coins)
{
     print "$value $coins{$value}<br />";
}

sortvalues.pl:

Nickel 0.05
Dime 0.1
Quarter 0.25

PERL - Add an Element

Adding a new key/value pair can be done with one line of code. This example is straight forward, we add the key/value pairs of penny/01 and half dollar/50 to the existing hash.

addelements.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";

# BEGINNING HASH
%coins = ( "Quarter" , .25,
           "Dime" ,    .10,
           "Nickel",   .05 );
# PRINT THE OLD HASH
while (($key, $value) = each(%coins)){
     print $key.", ".$value."<br />";
}

# ADD NEW ELEMENT PAIRS
$coins{Penny} = .01;
$coins{HalfDollar} = .50;

# PRINT THE NEW HASH
print "<br />";
while (($key, $value) = each(%coins)){
     print $key.", ".$value."<br />";
}

addelements.pl:

Nickel, 0.05
Dime, 0.1
Quarter, 0.25

Nickel, 0.05
Dime, 0.1
HalfDollar, 0.5
Penny, 0.01
Quarter, 0.25
Once again, you may have different results in the display box since there was no sort function passed to our hash. The important part is that the two new key/value pairs were added increasing your hash from 3 to 5 entries.

PERL - Remove an Element

With the delete function we can remove an element from our hash in a similar fashion as demonstrated above.

removeelements.pl:

#!/usr/bin/perl

print "content-type: text/html \n\n";

# DEFINED HASH
%coins = ( "Quarter" , .25,
           "HalfDollar" ,    .50,
           "Penny" ,    .01,
           "Dime" ,    .10,
           "Nickel",   .05 );

# PRINT OLD HASH
while (($key, $value) = each(%coins)){
     print $key.", ".$value."<br />";
}

# DELETE THE ELEMENT PAIRS
delete($coins{Penny});
delete($coins{HalfDollar});

# PRINT THE NEW HASH
print "<br />";
while (($key, $value) = each(%coins)){
     print $key.", ".$value."<br />";
}

removeelements.pl:

Nickel, 0.05
Dime, 0.1
HalfDollar, 0.5
Penny, 0.01
Quarter, 0.25

Nickel, 0.05
Dime, 0.1
Quarter, 0.25
Here we reversed the process, eliminating our penny and half dollar keys from our hash altogether. It is not necessary to remove the value, PERL has taken care of this for us automatically.

0 comments:

Post a Comment