LUXSCI
March 25th, 2010

Introduction to Internet Programming Part 6: Perl Data Part II – Hashes

[RAW]

This article is Part 6 of our series on Internet Programming. See the Introduction and Part 1, Introduction to HTML. This article covers the Perl “hash” data type.

Introduction to Hashes

A Perl “hash” is very similar to a Perl list — it is a way to
store a lot of data in one named variable. The difference is that
where as in lists, the “compartments” where you can store your
data are numbered 0, 1, 2, etc., in hashes the compartments each
have their own arbitrary name! The “name” of the
compartment is called a key and the information stored in
a compartment is called a value. So using hashes is all about
storing and retrieving keys and values.

Since you are already familiar with lists from Lesson 5, it
is easiest to show you examples of using a hash and explain how
it is different from how you would use a list….

    %hash = ( "name", "Joe Smith",
              "phone", "617-555-7890",
       	      "state", "Massachusetts",
              "country, "United States of America" );

    print $hash{"name"}, " - ", $hash{phone}, "\n";

    $hash{"state"} = "Maine";

    print $hash{"state"};

Creating a hash: You can create a hash using an
assignment just like you would create a list, the differences are:

  • The hash variable is prefixed with a percent sign (“%“)
    instead of the at-sign.

  • The list of data that you assign to the hash is no longer just
    a list of values, as in the case of a list. Now it is a list of
    “key,value” pairs. In the example above, we are creating a hash
    (called, creatively enough, “hash”) which contains 4 keys and
    4 values. The keys are “name”, “phone”, “state”, and “country”
    and the respective values are the items in the list immediately
    after each key. So, you can create a hash explicitly by assigning
    a list of the form (key1, value1, key2, value2, ....) to
    you hash variable.

Reading and writing hash elements: Just like when using
a list, when you read a value from or write a value to a hash, you
use the dollar sign (“$“) as the prefix because you are dealing
effectively with a normal simple variable. The only differences between
accessing lists and hashes is that you replace the square brackets containing
the index of the item, i.e. [3], with curly brackets containing
the name of the key, i.e. {"state"}. The quotation marks are
not always necessary, but it is good practice to use them so you are
not surprised when they are necessary.

Hashes are just like lists, but with names instead of numbers for
the indices. You are responsible for making up these names. The
names are case sensitive so $hash{State} and
$hash{"State"} refer to the values of two completely different
and independent hash keys. Use hash elements just like you would use
array elements or normal variables.

Hash Functions

Just like with lists, there are several functions built into Perl
that are designed to aid in your use of hashes. Some of these are:

keys
@list_of_keys = keys(%my_hash);

This function returns a list of all the keys being used in your hash.
It does not return any of the values, just the keys. Also, you have
no way of knowing what order it will return the keys in.

values
@list_of_values = values(%my_hash);

This function returns a list of all the values being stored at each key
in your hash.
It does not return any of the keys, just the values. Also, you have
no way of knowing what order it will return the values in (except
that it will be in the same other that keys returns the list of
keys).

exists
if (exists $my_hash{"questionable_key"}) {

The exists operator is used in conditional statements. It returns
“true” if there is such a key in your hash and “false” if your hash
has no such key. I.e. “Does this key exist?”.

defined
if (defined $my_hash{"questionable_key"}) {

The defined operator is used in conditional statements. It returns
“true” if there is such a key in your hash and the value is
defined (i.e. the values is NOT undef). It returns “false”
otherwise.

For example, a common use of the keys and values
functions is to iterate over and display everything in a hash:

    %hash = ( "name", "Joe Smith",
              "phone", "617-555-7890",
              "state", "Massachusetts",
              "country, "United States of America" );

   ## Iterate over and print out all key/value pairs

   @hash_keys = keys(%hash);

   for $key (@hash_keys) {
       print "Key '$key' has value '$hash{$key}'\n";
       }

   ## An alternate way...

   for $key (keys(%hash)) {
       print "Key '$key' has value '$hash{$key}'\n";
       }

   ## An alternate way...

   @hash_keys = keys(%hash);
   @hash_vals = values(%hash);

   for ($i=0;$i<=$#hash_keys;$i=$i+1) {
       print "Key '$hash_keys[$i]' has value '$hash_vals[$i]'\n";
       }

These are very common ways to iterate over a hash and print out all
of a hash's keys and values. Note that you don't need to use quotes
around the name of the hash key if it is stored in a variable, i.e.
$hash{$key}.

Also note that there is no concept of an "index" for a hash element,
there are only keys and values and it is up to you to define the keys.

What Good are Hashes?

At this point you may be asking "what good are hashes?" and
"why not just use lists for everything?". Well, you could use
lists for everything, but if you have a collection of information
that naturally has the form of key/value pairs, a hash is often
a better choice:

  • It is easier to store and retrieve the information
  • Using keys rather than numbers makes it easier to read your
    Perl program and avoid errors.

Good examples of the kinds of things that are "just made for hashes"
include:

  • Contact information about someone as in the example above
  • The information stored in an HTML form (all the fields have
    names and values)

  • Any list where it is easier to refer to the elements by
    a name rather than a number, such as a list of people and
    email addresses, or a list of companies and year-end earnings, etc.
    These can all be represented by a pair of lists, but the information
    can be retrieved more easily using a hash.

[/RAW]

Leave a Comment


You must be connected or logged in to post a comment. This is to reduce spam comments.

If you have not previously commented, you can connect using existing social media account, or register with a new username and password.

LUXSCI