Whay is the most elegant way to turn a hash inside out?
Whay is the most elegant way to turn a hash inside out?

By that I mean replace keys with values and vice versa (assuming that all the values are 100% unique).

E.g.

Start with

    %start = (1=>"a", 2=>"b", 3=>"c");
    
    # ...

    # PROFIT: 
    %finish = ("c" => 3, "b" => 2, "a" => 1);

I know I can do it the brute force way:

    foreach my $key (%start) {
        $value = $start{$key};
        $finish{ $value } = $key;
    }

But this can't be the most Perly-elegant way of doing so!

Whay is the most elegant way to turn a hash inside out?
===

Whay is the most elegant way to turn a hash inside out?

By that I mean replace keys with values and vice versa (assuming that all the values are 100% unique).

E.g.

Start with

    %start = (1=>"a", 2=>"b", 3=>"c");
    
    # ...

    # PROFIT: 
    %finish = ("c" => 3, "b" => 2, "a" => 1);

I know I can do it the brute force way:

    foreach my $key (%start) {
        $value = $start{$key};
        $finish{ $value } = $key;
    }

But this can't be the most Perly-elegant way of doing so!
