Hashes

Hashes are similar to arrays in that they are a collection of scalars. But whereas elements of arrays are accessed through their indexed positions, elements in hashes are looked up via a key string. Another name for hashes are associative arrays. You can think of a hash as a collection of pairs of scalar variables. Each pair consists of a key and a value. Keys must be unique because they identify the location of a value. Values inside a hash do not have to be unique. Hash variables begin with a '%'. Values inside a hash are accessed by referring to the hash variable in a scalar context and specifying the key.

%hash_variable;                    # a hash variable
$hash_variable{$key} = $value;     # a value stored in a hash variable


Construction

A hash can be constructed from a list with an even number of elements. The first element of the list becomes a key. The second element because the value for that key. Each succeeding pair of elements, likewise, becomes a key-value pair in the hash.

Example

%cities_and_states = ('Corvallis', 'Oregon', 
                      'Seattle', 'Washington', 
                      'Portland', 'Oregon', 
                      'Boise', 'Idaho');
print $cities_and_states{'Boise'} . "\n";
print $cities_and_states{'Corvallis'} . "\n";

Output:

Idaho
Oregon

A hash can also be constructed - or modified - by merely assigning values to keys. The following contructs a hash indentical to the hash above.

$cities_and_states{'Corvallis'} = 'Oregon';
$cities_and_states{'Portland'} = 'Oregon';
$cities_and_states{'Boise'} = 'Idaho';
$cities_and_states{'Seattle'} = 'Washington';


Notice that the order in which values are put into a hash does not matter because the values are accessed only via the keys.


Modifying hashes

Any value stored in a hash can be changed by assigning to it via its key.

$cities_and_states{'Portland'} = 'Oregon';   # Hash now contains the key-value pair
                                             # Portland => Oregon

$cities_and_states{'Portland'} = 'Maine';    # Hash now contains the key-value pair
                                             # Portland => Maine
                                             # Oregon has been replaced by Maine   

To delete a key-value pair in a hash variable, use the delete() function on the key

delete($cities_and_states{'Boise'});


Hash operations

The functions keys() and values() return all the keys and values, respectively, of the hash as a list.

Example:

@all_keys = keys(%cities_and_states);
@all_values = values(%cities_and_states):


Here's how you can iterate through all the elements of a hash:

@all_keys = keys(%cities_and_states);
foreach $key (@all_keys){
	# some statements here, such as
	print "$key, $cities_and_states{$key}\n";
}

Output:

Seattle, Washington
Corvallis, Oregon
Boise, Idaho
Portland, Oregon

The order in which these pairs is printed depends on the order of the keys. You don't have any control over the order in which the keys are generated via the key() function. You could, however, perform a sort() command on the keys to put them in alphabetical order before going into the foreach loop.

@all_keys = sort(keys(%cities_and_states));
foreach $key (@all_keys){
	# some statements here, such as
	print "$key, $cities_and_states{$key}\n";
}


Output:

Boise, Idaho
Corvallis, Oregon
Portland, Oregon
Seattle, Washington