campbell has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I have a hash, and I would like it so that no two keys are the same. Any ideas about how I might arrange this?

Campbell

Replies are listed 'Best First'.
Re: equal keys in a hash
by Mr. Muskrat (Canon) on Jun 26, 2003 at 16:41 UTC

    A hash will never have two identical keys.

    #!/usr/bin/perl -w use strict; my %hash = ( foo => 1, bar => 2, baz => 3, foo => 4, ); print $hash{foo}, "\n"; # prints 4

Re: equal keys in a hash
by Tanalis (Curate) on Jun 26, 2003 at 16:40 UTC
    In any normal hash, no two keys can be the same anyway. Assuming that you mean that you need to check that a key doesn't already exist, you can use exists, like this:
    my %hash; my $key = "test"; if (exists $hash{$key}) { # do something } else { # do something else }
    You're question's not particularly clear, and that's a complete stab in the dark *grin* - could you post a little more about what it is you're trying to achieve?

    -- Foxcub
    #include www.liquidfusion.org.uk

Re: equal keys in a hash
by Itatsumaki (Friar) on Jun 26, 2003 at 16:40 UTC

    By definition, keys within a hash are unique. You might want to do some reading on this.

    -Tats
Re: equal keys in a hash
by hardburn (Abbot) on Jun 26, 2003 at 16:39 UTC

    What do you mean by 'keys'? In the definition of 'key' usually used with hashes, it's impossible to have duplicate keys (that's just how hashes work). If you mean values, look below.

    Reverse the hash, then reverse it again:

    my %hash_with_duplicates: # Intitilized elsewhere my %hash_without_duplicates = reverse reverse %hash_with_duplicates;

    When running a hash through reverse(), it reverses the name/value pairs so that the values become the names. This gets rid of duplicate values. Reversing it again returns us to the orginal hash, but without duplicate values. The only problem is that you have no control over which duplicate value gets deleted.

    Update: The above doesn't do what I expected. See replies.

    My guess is that Perl sees the result from the first reverse (taken in order of execution) as a list, and thus doesn't treat it by the rules of a hash (i.e., no duplicate keys). The second reverse will reverse it as a plain list, not a hash, which is then assigned to a hash. Again, see the replies below for something that will work.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      Did you test this?

      Surely you need to assign the reversed list to a hash to get rid of the duplicates? ie
      %intermediate_hash = reverse %hash_with_duplicates; %hash_without_duplicates = reverse %intermediate_hash;
      Your code just recreates the original hash quite expensively.

      Jasper

        Did you test this?

        No. Did you read my .sig? :)

        To my surprise, you're right. It does just recreate the orginal without getting rid of duplicates. To avoid the intermediate hash, you have to force the first reverse (taken in order of execution) to return a referance to hash, which is then immediately dereferanced:

        my %hash_without_duplicates = reverse %{ +{ reverse %hash_with_duplica +tes } };

        I actually have tested the above :)

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

        Or, doing it anonymously without the extra hash:

        %hash_without_duplicates = reverse %{ { reverse %hash_with_duplicates} };

        -xdg