Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^4: inverting hash / grouping values

by LanX (Bishop)
on Sep 27, 2013 at 18:26 UTC ( #1056058=note: print w/replies, xml ) Need Help??

in reply to Re^3: inverting hash / grouping values
in thread inverting hash / grouping values

Thanks, fascinating ...

...but maybe I was not still not clear enough! ;-)

I'd rather prefer a solution better (readable | self documenting | maintainable) then

DB<140> \%h => { a => 1, b => 2, c => 1, d => 2, e => 3 } DB<141> push @{ $h2{$h{$_}} }, $_ for keys %h => "" DB<142> \%h2 => { 1 => ["c", "a"], 2 => ["b", "d"], 3 => ["e"] }

so not necessarily a complicated solution only relying on List::Util or List::MoreUtils ...

BUT I know that you love this kind of games ... ;D

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^5: inverting hash / grouping values
by hdb (Monsignor) on Sep 28, 2013 at 08:26 UTC

    Yes, I do. And I think I have understood what you are looking for. But I do not think it is possible. In order to go from your hash to the inverted one, one has to look across elements, so most functionals will not work.

    Best would be to just add your code to some of the core modules, but then it would be in competition with Hash::MoreUtils which does - as you point out below - the same but not quite.

    UPDATE: I can hide the push in reduce. But I don't think that this is what you are looking for either...

    use strict; use warnings; use List::Util 'reduce'; use Data::Dumper; my %t = ( a => 1, b => 2, c => 1, d => 2, e => 1, f => 2, g => 3 ); my $i = reduce { $a = { $t{$a} => [ $a ] } unless ref $a; push @{ $a->{ $t{$b} } }, $b; $a } keys %t; print Dumper $i;
      > Best would be to just add your code to some of the core modules

      not that easy I'm still struggling to see the best API for a set of functionals operating on hashes.

      for instance my function invert should be renamed because you can do %h2= invert invert %h such that you get the original back. so maybe hash_part or hpart might be better.

      To have a real "mathematical" invert one has to consider a special structure relation which is effectively a HoA (simple hashes are "functions" which can only be inverted if they are bijective i.e. have unique values)

      DB<131> sub invert { my %h=@_; my %h2; while ( my ($k,$v) = each %h ) { push @{ $h2{$_} } , $k for @$v; } return %h2; } DB<134> %t = (1 => ["e", "c", "a"], 2 => ["b", "d", "f"], 3=>[a] ) => (1, ["e", "c", "a"], 2, ["b", "d", "f"], 3, ["a"]) DB<135> invert %t => ("e", [1], "c", [1], "a", [1, 3], "b", [2], "d", [2], "f", [2]) DB<136> invert invert %t => (1, ["e", "c", "a"], 3, ["a"], 2, ["b", "d", "f"])

      Now taking the last use cases we saw, I'm not sure how this special application of invert would help.

      AND I had to change the interface, to be chainable.

      Originally I was operating on a hash_reference and not a list.

      DWIM isn't easy here, prototypes don't allow mixing '%hashes' and LISTs, such that the former is taken as reference.

      (One might pass an explicit reference '\%hash' and check if the LIST has just one element. Well not very elegant ...)

      Hope you understand my struggles...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1056058]
[erix]: o/
[erix]: I'm a bit stuck with regex capturing. Anyone wants to ahve a look at my [erix://pad | scratchpad ? (it's the little test program at the top there)]
[erix]: hm [erix://pad]
[erix]: pad::/erix
[erix]: erix's scratchpad

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-01-19 08:05 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (216 votes). Check out past polls.