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 like the following:

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

In this example, you assign to 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. $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, a scalar value, you precede the variable name with a dollar sign ($) indicating a single value is being referenced, and you use the braces to indicate the value. To Perl, $Authors{'Dune'} represents a single scalar value—in this case, Frank Herbert.

Hashes with one key aren'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' );

Note

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.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.38.41