Lesson 6a - Hashes

Summary:
What are hashes?
How can I use them?
How can I manipulate their contents?

Sometimes storing data in a sequential order isn't the best way to go about a task. Hashes allow data to be stored by reference to 'keys', or strings associated with those particular data. An examplary use of hashes would be storing a list of characters associated with the name of a television series.

Creating a Hash

Hashes are named beginning with the `%` character, like `%my_hash`. Initializing a hash is much like initializing an array, but rather than just providing a list of elements, one has to provide a 'key' for each element. The key must be followed by the data to be stored with that key. Example:

-------------------------------------
%characters = (
'Simpsons', 'bart marge homer lisa maggie',
'Southpark', 'kyle stan cartman kenny'
);

-------------------------------------

When key/data combinations are separated like this on separate lines, it's easy to tell them apart. However, using commas like this can be confusing if working with arrays at the same time, so `=>` can be used as a separator for keys and data to make your code more readable. This example does exactly the same thing:

-------------------------------------
%characters = (
'Simpsons' => 'bart marge homer lisa maggie',
'Southpark' => 'kyle stan cartman kenny'
);

-------------------------------------


Accessing Data in a Hash

Data in a hash is accessed like data in an array, however, curly brackets are used instead of square brackets and the key is substituted for the array subscript. Example:

-------------------------------------
%currency = (
'Canada' => 'Dollar',
'USA' => 'Dollar',
'UK' => 'Pound Sterling',
'Germany' => 'Mark'
'Sweden' => 'Kroner'
);
print $currency{UK} . "\n"; # Prints "Pound Sterling".
$currency{Germany} = 'Deutsche Mark'; # Modifies entry for 'Germany'.

-------------------------------------

To delete a key and its associated value from a hash, use the `delete` function. All you need to do is specify the string you'd use to access that key in the hash, e.g. `$hash{key}`. Note that this is different from simply assigning a null string (`''`) to that key. Example:

-------------------------------------
# ... using our %currency hash from above ...
delete $currency{USA}; # There is no longer a USA key, and the 'Dollar'
# value associated with it no longer exists either.
# Note that this does not affect the 'Dollar' value
# associated with the Canada key.

-------------------------------------


The `keys` and `values` Functions

`keys` and `values` return arrays containing, respectively, the keys and associated data from the specified hash. They are sorted in an apparently random order, but the order is the same for both functions. These functions are more useful (for example) when working with for loops, which will be discussed in the flow control section. Example:

-------------------------------------
# ... still using %currency ...
@currency_keys = keys %currency;
@currency_values = values %currency;
# $currency_values[$n] will be the value for $currency_keys[$n] for the same
# value of $n.

-------------------------------------


The `%ENV` Hash

The `%ENV` hash contains the current shell environment, with each environment variable being linked to a key with its name. In a CGI context, `%ENV` can be used to retrieve things such as the query string (i.e. http://my.server.com/cgi-bin/script.cgi?this+is+the+query+string) and other miscellaneous information.

Lesson 7 - Flow Control

Home | Lessons | Get Perl | Resources