Unfortunately, this isn't a relationship of equals. The relationship
encoded in a hash is that of the genitive case or the possessive,
like the word "of " in English, or like "'s". We could encode that
the boss of Nat is Tim. Hashes only give
convenient ways to access values for Nat's boss; you can't ask whose
boss Tim is. Finding the answer to that question is a recipe in this
chapter.
Fortunately, hashes have their own special benefits, just like
relationships. Hashes are a built-in data type in Perl. Their use
reduces many complex algorithms to simple variable accesses. They are
also fast and convenient to build indices and quick lookup tables.
Unlike a regular comma, the comma arrow has a special property: it
quotes any word preceding it, which means you can safely omit the
quotes and improve legibility. Single-word hash keys are also
automatically quoted when they occur inside braces, which means you
can write $hash{somekey} instead of
$hash{"somekey"}. You could rewrite the preceding
initialization of %food_color as:
%food_color = (
Apple => "red",
Banana => "yellow",
Lemon => "yellow",
Carrot => "orange"
);
One important issue to be aware of regarding hashes is that their
elements are stored in an internal order convenient for efficient
retrieval. This means that no matter what order you insert your data,
it will come out in an unpredictable disorder.