in reply to how to use the values of the first hash to be the keys for the second hash

Do you mean something like this?

use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); my %a = qw(key1 val1 key2 val2 key3 val3); my %b = qw(val1 val5 val2 val6 val3 val7 val4 val8); my %c = %{{map {($_, $b{$a{$_}})} keys %a}}; pp(\%c)

Produces:

{ key1 => "val5", key2 => "val6", key3 => "val7" }

Replies are listed 'Best First'.
Re^2: 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 15:51 UTC

    thank you guys for the quick reply

    for my hash1

    I have "key1" with values "val1, val2",

    "key2 withs value "val3".

    each of the value in hash1, is key in hash2:

    for example: "val1" in hash1 has values "val4, val5, val6" in hash2; and so on.

    What I need is to generate a hash to combine the first two hashes, something like the following:

    "key1" with values "val4,val5,val6,val7, val8";

    "key2" with value "val3".

      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"]], }

        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.