Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^2: inverting hash / grouping values

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

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

Thanks, but I wasn't clear enough!

I would like to see an elegant solution, that is w/o deeply nested constructs like map within map.

IMHO one of the striking points of chaining simple functions is better readability compared to nested solutions.

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^3: inverting hash / grouping values
by hdb (Monsignor) on Sep 27, 2013 at 18:13 UTC

    Next attempt:

    use strict; use warnings; use List::Util 'reduce'; use List::MoreUtils 'mesh'; use Data::Dumper; my %t = ( a => 1, b => 2, c => 1, d => 2, e => 1, f => 2, g => 3 ); my( $k, $v ) = @{ +reduce { if( $a->[0]->[-1] eq $b->[0]->[0] ) { push @{$a->[1]->[-1]}, $b->[1]->[0]->[0]; } else { push @{$a->[0]}, $b->[0]->[0]; push @{$a->[1]}, [$b->[1]->[0]->[0]]; } $a; } map { [ [$t{$_}], [[$_]] ] } sort { $t{$a} <=> $t{$b} } keys %t } +; my %i = mesh @$k, @$v; print Dumper \%i;
      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)

        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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055996]
[ambrus]: No, you're not doing anything wrong. It's just that our automatic spam filters confuse you with the spammers who post advertisments for online shops of counterfeit branded clothing.
[ambrus]: Or unless you left the title field empty or entered a very short title for the node, but then you'd get a message saying that.
[ambrus]: I hope Corion or some other admin is here and can check the logs to see what the problem is.
[mz2255]: I wish I had an online shop but sadly no. The title field definitly wasn't short, had a perl module in the title with 5-6 additional words.
[ambrus]: you can also try to just post again in case it was some intermittent error
[mz2255]: Tried again, denied. Maybe my code is just so bad the site refuses to post it.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2017-10-19 15:36 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (255 votes). Check out past polls.