Perl Cookbook

Perl CookbookSearch this book
Previous: 5.7. Hashes with Multiple Values Per KeyChapter 5
Hashes
Next: 5.9. Sorting a Hash
 

5.8. Inverting a Hash

Problem

Hashes map keys to values. You have a hash and a value for which you want to find the corresponding key.

Solution

Use reverse to create an inverted hash whose values are the original hash's keys and vice versa.

# %LOOKUP maps keys to values
%REVERSE = reverse %LOOKUP;

Discussion

This technique uses the list equivalence of hashes mentioned in the introduction. In list context, reverse treats %LOOKUP as a list and reverses the order of its elements. The significant property of a hash treated as a list is that the list elements come in pairs: the first element is the key; the second, the value. When you reverse such a list, the first element is the value, and the second is a key. Treating this list as a hash results in a hash whose values are the keys of the original hash and vice versa.

Here's an example:

%surname = ( "Mickey" => "Mantle", "Babe" => "Ruth" );
%first_name = reverse %surname;
print $first_name{"Mantle"}, "\n";
Mickey

When we treat %surname as a list, it becomes:

("Mickey", "Mantle", "Babe", "Ruth")

(or maybe ("Babe", "Ruth", "Mickey", "Mantle") because we can't predict the order). Reversing this list gives us:

("Ruth", "Babe", "Mantle", "Mickey")

When we treat this list as a hash, it becomes:

("Ruth" => "Babe", "Mantle" => "Mickey")

Now instead of turning first names into surnames, it turns surnames into first names.

Example 5.2 is a program called foodfind. If you give it a food name, it'll tell you the color of that food. If you give it a color, it'll tell you a food of that color.

Example 5.2: foodfind

#!/usr/bin/perl -w
# foodfind - find match for food or color

$given = shift @ARGV or die "usage: foodfind food_or_color\n";

%color = ( 
           "Apple"  => "red",
           "Banana" => "yellow",
           "Lemon"  => "yellow",
           "Carrot" => "orange"
         );

    %food = reverse %color;

    if (exists $color{$given}) {
    print "$given is a food with color $color{$given}.\n";
    }
    if (exists $food{$given}) {
    print "$food{$given} is a food with color $given.\n";
    }

If two keys in the original hash have the same value (as "Lemon" and "Banana" do in the color example), then the inverted hash will only have one (which is dependent on the hashing order, and you shouldn't try to predict it). This is because hashes have, by Perl definition, unique keys.

If you want to invert a hash with non-unique values, you must use the techniques shown in Recipe 5.7. That is, build up a hash whose values are a list of keys in the original hash:

# %food_color as per the introduction
while (($food,$color) = each(%food_color)) {
    push(@{$foods_with_color{$color}}, $food);
}

print "@{$foods_with_color{yellow}} were yellow foods.\n";
Banana Lemon were yellow foods.

This also lets us change the foodfind program to handle colors represented by more than one food. For instance, foodfind yellow reports bananas and lemons.

If any values in the original hash were references instead of just strings and numbers, the inverted hash poses a problem because references don't work well as hash keys unless you use the Tie::RefHash module described in Recipe 5.12.

See Also

The reverse function in perlfunc (1) and in Chapter 3 of Programming Perl; Recipe 13.15


Previous: 5.7. Hashes with Multiple Values Per KeyPerl CookbookNext: 5.9. Sorting a Hash
5.7. Hashes with Multiple Values Per KeyBook Index5.9. Sorting a Hash

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.