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

In a utility-module I have the folling snippet (it's a slightly extended utils.pm of the nagios-distribution):

%ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=> +4); %ERROR_TEXT= map { $ERRORS{$_} => $_ } keys %ERRORS;

Is there a more concise way to 'reverse' a hash?

regards,
tomte


Hlade's Law:

If you have a difficult task, give it to a lazy person --
they will find an easier way to do it.

Replies are listed 'Best First'.
Re: 'reversing' a hash (reverse)
by particle (Vicar) on Jun 18, 2003 at 17:02 UTC
    %ERROR_TEXT= reverse %ERRORS;

    see reverse for more info. by the way, it's usually called 'inverting' a hash.

    ~Particle *accelerates*

Re: 'reversing' a hash
by dws (Chancellor) on Jun 18, 2003 at 17:05 UTC
    Is there a more concise way to 'reverse' a hash?

      %ERROR_TEXT = reverse %ERRORS; is the idiomatic way. It's more concise, but it liable to catch a few less seasoned readers by surprise.

Re: 'reversing' a hash
by shotgunefx (Parson) on Jun 18, 2003 at 21:46 UTC
    Just a thought, using reverse as noted above is the easiest way, though if you're worried about duplicate keys, you can always use a
    foreach(keys %hash ){ } or while (my ($k,$v) = each %hash){ }
    so you can handle duplicates in a reasonable way.

    -Lee

    "To be civilized is to deny one's nature."
Re: 'reversing' a hash
by BrowserUk (Patriarch) on Jun 18, 2003 at 22:10 UTC

    I can't thing of any particular advantage for this but...

    @inverted{ values %hash } = keys %hash;


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      I couldn't resist ;-)

      use Data::Dumper; use Benchmark qw(:all); %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=> +4); cmpthese(1000000, { 'map' => sub { %ERRORS_TEXT = map { $ERRORS{$_} => $_ } keys %ERRORS; }, 'reverse' => sub { %ERRORS_TEXT = reverse %ERRORS; }, 'values' => sub { @ERRORS_TEXT{ values %ERRORS } = keys %ERRORS; }, } ); __END__ First run: Benchmark: timing 1000000 iterations of map, reverse, values... map: 27 wallclock secs (24.66 usr + 0.03 sys = 24.69 CPU) @ 40 +502.23/s (n=1000000) reverse: 10 wallclock secs ( 9.03 usr + 0.00 sys = 9.03 CPU) @ 11 +0741.97/s (n=1000000) values: 8 wallclock secs ( 7.70 usr + 0.04 sys = 7.74 CPU) @ 12 +9198.97/s (n=1000000) Rate map reverse values map 40502/s -- -63% -69% reverse 110742/s 173% -- -14% values 129199/s 219% 17% -- Second run: Benchmark: timing 1000000 iterations of map, reverse, values... map: 27 wallclock secs (24.49 usr + 0.03 sys = 24.52 CPU) @ 40 +783.03/s (n=1000000) reverse: 8 wallclock secs ( 8.54 usr + 0.02 sys = 8.56 CPU) @ 11 +6822.43/s (n=1000000) values: 7 wallclock secs ( 7.55 usr + 0.01 sys = 7.56 CPU) @ 13 +2275.13/s (n=1000000) Rate map reverse values map 40783/s -- -65% -69% reverse 116822/s 186% -- -12% values 132275/s 224% 13% -- Third run: Benchmark: timing 1000000 iterations of map, reverse, values... map: 26 wallclock secs (24.37 usr + 0.06 sys = 24.43 CPU) @ 40 +933.28/s (n=1000000) reverse: 11 wallclock secs ( 8.63 usr + 0.03 sys = 8.66 CPU) @ 11 +5473.44/s (n=1000000) values: 9 wallclock secs ( 7.57 usr + 0.01 sys = 7.58 CPU) @ 13 +1926.12/s (n=1000000) Rate map reverse values map 40933/s -- -65% -69% reverse 115473/s 182% -- -12% values 131926/s 222% 14% --

      Looks like I could gain a wee bit of speedup using your code, if it would matter, which it doesn't in my particular case; for the loss of readability, though IMHO :)

      regards,
      tomte


      Hlade's Law:

      If you have a difficult task, give it to a lazy person --
      they will find an easier way to do it.

        Wow! A whole 1.3 microseconds saved. And to think I didn't see any merit in it:)

        On a slightly serious note, I think a novice perler might actually find the "values become keys and keys become values" slightly more obvious than the reverse method, which relies on a pretty good understanding of how hashes work in list contexts, on their first viewing, but once you understand that, the presence of the big clue, R-E-V-E-R-S-E does make it pretty clear:)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: 'reversing' a hash
by Tomte (Priest) on Jun 18, 2003 at 17:06 UTC

    ++to you both, I'd known it ;-)
    Man, do I just love perl.

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

A reply falls below the community's threshold of quality. You may see it by logging in.