http://www.perlmonks.org?node_id=1055994


in reply to inverting hash / grouping values

Have you looked at the safe_reverse function in Hash::MoreUtils?

#! perl use strict; use warnings; use Data::Dump; use Hash::MoreUtils qw(safe_reverse); my %t = ( a => 1, b => 2, c => 1, d => 2, e => 1, f => 2 ); dd \%t; my %dup_rev = safe_reverse \%t; dd \%dup_rev;

Output:

0:56 >perl 729_SoPW.pl { a => 1, b => 2, c => 1, d => 2, e => 1, f => 2 } { 1 => ["c", "e", "a"], 2 => ["b", "d", "f"] } 0:57 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: inverting hash / grouping values
by LanX (Saint) on Sep 27, 2013 at 15:13 UTC
    > Have you looked at the safe_reverse function in Hash::MoreUtils?

    Yes I looked - but I didn't install to try ... hmm thanks!

    IMHO the documentation is quite misleading ...

    Returns safely reversed hash (value, key pairs of original hash). If no BLOCK is given, following routine will be used:

    (value,key pairs ???)

    ... and better examples showing in and output are lacking.

    After looking at it I was unhappy about the blocked namespace, a module called Hash::MoreUtils could offer much more.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    update

    After diving into the code, it looks like safe_reverse will not represent unique values as 1-element-arrays but as scalars.

    update

    This partly explains my confusion, this function works like a normal reverse if all values are unique.

    DB<100> use Hash::MoreUtils qw/safe_reverse/ DB<101> @h{a..c}=1..3 => (1, 2, 3) DB<102> safe_reverse \%h => (1, "a", 3, "c", 2, "b") DB<105> @h{a..c}=(1,1,2) => (1, 1, 2) DB<106> safe_reverse \%h => (1, ["a", "b"], 2, "c")

    to be able to have the same effect like invert in the OP one needs to write

    DB<110> safe_reverse sub { my ($k, $v, $r) = @_; return [ @{$r->{$v} +}, $k ] },\%h => (1, ["a", "b"], 2, ["c"])
      DB<106> safe_reverse \%h => (1, ["a", "b"], 2, "c")

      That kind of hash (reference) drives me nuts as it makes using the values cumbersome due to the need to test the type before use. (That reminds me to write a utility sub to convert (plain) scalar to one-element array reference.)

      I'm confused by your question. I have seen a key and its value frequently refered to as a key/value pair.

      Do I misunderstand you?

        > I have seen a key and its value frequently refered to as a key/value pair.

        the result of this inversion operation is a HoA  { key1 => [ value11, value12], key2 => [value21],  ...}

        And neither

        • a hash with key/value pairs { key1 =>value1 , key2 => value2, ...}
        • a list with key/value pairs ( key1, value1, key2, value2 ,...)
        • a list of arrays with pairs (tuples) ( [key1, value1], [key2,value2], ... )

        IMHO all better interpretations.

        Cheers Rolf

        ( addicted to the Perl Programming Language)