Online Learning Applications for Technical English


Home
Syllabus
Assignments

Instructor



Producing Homepages

Hashes

A hash is a collection of values accessed with string keys, unlike an array which uses integer indices. The name of a hash begins with the percent sign.

For example, let us make a hash of names and e-mail addresses:

Aki aki@www
Ben btanaka@mail
Cathy cat@yahoo

To assign the values to a hash:

# Write the hash as a list of key, value pairs.
  %names_list = ( "Aki", "aki@www",
    "Ben", "btanaka@mail", 
    "Cathy", "cat@yahoo" );             # Style 1
You can also use the big arrow (=>) to write the assignment:
# Big arrow (=>) replaces comma for readability.
  %names_list = ( "Aki" => "aki@www",
    "Ben" => "btanaka@mail",
    "Cathy" => "cat@yahoo" );           # same result as Style 1
or you can assign elements one at a time:
  $names_list{'Aki'} = 'aki@www';
  $names_list{'Ben'} = 'btanaka@mail';
  $names_list{'Cathy'} = 'cat@yahoo';   # same result as Style 1

Note 1: The hash keys (indices) must be unique.

[ Back ]