in reply to Re^2: how to use the values of the first hash to be the keys for the second hash
in thread how to use the values of the first hash to be the keys for the second hash

use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); my %h1 = (key1=>[qw(1.1.1 1.1.2 1.1.3)], key2=>[qw(1.2.1 1.2.2 1.2.3)] +); my %h2 = (key1=>[qw(2.1.1 2.1.2 2.1.3)], key2=>[qw(2.2.1 2.2.2 2.2.3)] +); my %c; for my $h(\%h1, \%h2) {push @{$c{$_}}, $h->{$_} for keys %$h; } pp(\%c)

Produces:

{ key1 => [["1.1.1", "1.1.2", "1.1.3"], ["2.1.1", "2.1.2", "2.1.3"]], key2 => [["1.2.1", "1.2.2", "1.2.3"], ["2.2.1", "2.2.2", "2.2.3"]], }

Replies are listed 'Best First'.
Re^4: how to use the values of the first hash to be the keys for the second hash
by lrl1997 (Novice) on Sep 07, 2012 at 16:34 UTC

    Thank you philiprbrenan.

    But for %h2, the keys are supposed to be values in %h1

    i.e the keys for %h2 are:

    1.1.1

    1.1.2

    1.1.3

    1.2.1

    1.2.2

    1.2.3

    For each of the key, e.g 1.1.1, it would have different values in %h2: e.g.:

    1.1.1=> qw(3.3.1,3.3.2,3.3.3)

    1.1.2=> qw(4.4.1, 4.4.2, 4.4.3)

    1.1.3=> qw(5.5.1, 5.5.2, 5.5.3)

    1.2.1=> qw(6.6.1, 6.6.2, 6.6.3)

    1.2.2=> qw(7.7.1, 7.7.2, 7.7.3)

    1.2.3=> qw(8.8.1, 8.8.2, 8.8.3)

    so the end results i desire would be:

    key1=>qw(3.3.1,3.3.2,3.3.3, 4.4.1, 4.4.2, 4.4.3, 5.5.1, 5.5.2, 5.5.3)

    key2=>qw(6.6.1, 6.6.2, 6.6.3,7.7.1, 7.7.2, 7.7.3,8.8.1, 8.8.2, 8.8.3)

    That is that the keys from %h1 will have values from %h2.