This is a hash that can emulate duplicate keys. One module on CPAN allows you do this (and keep insertion order). It is Tie::DxHash. However my method allows you to add multiple values to one hash key, in essence, creating a hash that acts like it has duplicate keys. You can then check if your key has more than one value associated with it.
my %hash; # Assume a loop here that creates keys and values... my $key = 'alphabet'; my $letters = ['abc','def']; if (exists $hash{$key}) { my @letters_array = $hash{$key}; push @letters_array, $letters; my $new_val = [@letters_array]; $hash{$key} = $new_val; } else { $hash{$key} = $letters; } # Then check for "duplicate" keys (ie. a key with more than one value) +. for $key (keys %found_mod) { if (ref($found_mod{$key}[0]) eq 'ARRAY') { print "This key -- $key -- has more than one value!\n"; } }

Replies are listed 'Best First'.
Re: Hashes with "duplicate" keys
by Tanktalus (Canon) on Sep 07, 2005 at 00:03 UTC

    Been there, done that. And found you need a bit more than you've got. ;-)

    What you're doing works fine for a maximum of two values for a given key. But you need to check if the value already is an array ref:

    if (exists $hash{$key}) { if (ref $hash{$key} and ref $hash{$key} eq 'ARRAY') { push @{$hash{$key}}, $new_val; } else { my @letters_array = $hash{$key}; push @letters_array, $letters; my $new_val = [@letters_array]; $hash{$key} = $new_val; } } else { $hash{$key} = $letters; }
    Of course, this becomes a problem if an ARRAY is a legitimate value to be stored in the array, as it is in your example. Now you have to decide what an array means here: does it mean that someone has pre-populated the value with some stuff, and may be adding one or more keys to the list?
    my @pushed = (ref $letters && ref $letters eq 'ARRAY') ? @$letters : + $letters; push @{$hash{$key}}, @pushed;
    Or is each array ref a legitimate independant value? In this case, it's probably easiest if you treat all keys as always collided. This means that you always use an array. Then you can replace the whole if clause with:
    push @{$hash{$key}}, $letters;
    And then to check for actual collisions, you can do this:
    for $key (keys %found_mod) { if (@{$found_mod{$key}} > 1) { print "This key -- $key -- has more than one value!\n"; } }
    As you can see, I've been down this road before. One place you see this type of problem (or at least, one place I've seen it ;-}) is in XML-like hierarchical data where elements can be repeated. For example, XML, HTML, etc.