HTML CSS PHP PERL

Filling Your Hash

 
Previous Table of Contents Next

Filling Your Hash

Individual hash elements are created by assigning values to them, much as with array elements. For example, you can create individual hash elements, as in the following:

$Authors{'Dune'}='Frank Herbert';

In this example, you assign to an element in the hash %Authors. The key for this element is the word Dune, and the data is the name Frank Herbert. This assignment creates a relationship in the hash between Dune and Frank Herbert. THe value associated with the key, $Authors{'Dune'}, can be treated like any other scalar; it can be passed to functions, modified by operators, printed, or reassigned. When you're changing a hash element, always remember that you're modifying the value stored in the hash element, not the hash itself.

Why does the example use $Authors{} instead of %Authors{}? Like arrays, when hashes are represented as a whole, they have their own marker in front of the variable name (%). When you access an individual element of a hash, you are accessing a scalar value, so you precede the variable name with a dollar sign ($) indicating a single value is being referenced, and you use the braces around the key to indicate that you mean the value in the hash associated with that key, not an element in an array or an unrelated scalar with the same name as the hash. To Perl, $Authors{'Dune'} represents a single scalar valuein this case, Frank Herbert.

A hash with just one key isn't particularly useful. To put several values into a hash, you could use a series of assignments, as shown in the following:

$food{'apple'} = 'fruit';
$food{'pear'} = 'fruit';
$food{'carrot'} = 'vegetable';

To make this operation shorter, you can initialize the hash with a list. The list should consist of pairings of keys and values, as shown here:

%food = ('apple', 'fruit', 'pear', 'fruit', 'carrot', 'vegetable');

This example looks similar to array initializations discussed in Hour 4, "Stacking Building Blocks: Lists and Arrays." In fact, as you'll learn later in this hour, hashes can be treated as a special kind of array in many contexts.

When you're initializing a hash, keeping track of which items are keys and which items are values in a large list can be confusing. Perl has a special operator called a comma-arrow operator, =>. Using the => operator and taking advantage of the fact that Perl ignores whitespace, you can write hash initializations like the following:

%food = ( 'apple' => 'fruit',
     'pear'   => 'fruit',
     'carrot' => 'vegetable',
    );

Perl programmers, holding laziness as a virtue, have two additional shortcuts for hash initializations. The left side of the => operator is expected to be a simple string and does not need to be quoted. Also, a single-word hash key inside the curly braces is automatically quoted. So the initializations shown previously become the following:

$Books{Dune} = 'Frank Herbert';
%food =( apple => 'fruit',  pear => 'fruit',  carrot => 'vegetable' );

By the Way

The comma-arrow operator is called that because it acts like a comma (when it is separating list items) and it looks like an arrow.


    Previous Table of Contents Next
    © 2000- NIV