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

Can you show the script? You probably assign istead of pushing.

Update: Or, you can use map:

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %hash1 = (key1 => [qw(val1 val2)], key2 => ['val3'], ); my %hash2 = (val1 => [qw(val5 val6 val7)], val2 => [qw(val8 val9)], val3 => ['val3'], ); print Dumper { map { $_ => [ map @{ $hash2{$_} } , @{ $hash1{$_} } ] } keys %hash1 };
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re: how to use the values of the first hash to be the keys for the second hash
  • Download Code

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 18:09 UTC

    Thank you so much.

    Just I don't know how to print the result as the normal tab delimited table. Sorry for these newbie's questions.Really appreciate your help.

      You can use each to iterate over the hash:
      #!/usr/bin/perl use warnings; use strict; my %hash1 = (key1 => [qw(val1 val2)], key2 => ['val3'], ); my %hash2 = (val1 => [qw(val5 val6 val7)], val2 => [qw(val8 val9)], val3 => ['val3'], ); my %result = (map { $_ => [ map @{ $hash2{$_} } , @{ $hash1{$_} } ] } keys %hash1); while (my ($k, $v) = each %result) { print $k, " @$v\n"; }
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        This is so great.

        But the values in my hash1 are not arrays, they are strings with each key for hash2 separated by comma ",". How should I turn the strings in to array so that I could adapt to the code?

        Thanks again, great help!

      and, since value's of my hash tables are separated by ",", how can I do map by splitting values in hash1 by ","?