in reply to Re: equal keys in a hash
in thread equal keys in a hash

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

Replies are listed 'Best First'.
Re: Re: Re: equal keys in a hash
by hardburn (Abbot) on Jun 26, 2003 at 17:38 UTC

    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

Re: Re: Re: equal keys in a hash
by xdg (Monsignor) on Jun 26, 2003 at 17:42 UTC

    Or, doing it anonymously without the extra hash:

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

    -xdg