satzbu has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How do I make all the keys in %a with common keys in %b equal to %b's values?

Replies are listed 'Best First'.
Re: store a config file in hash
by Corion (Patriarch) on Mar 23, 2010 at 10:12 UTC

    Please show us what code you have already written (30 lines maximum), what input you give it (5 lines) and what output you get, and also what output you expect.

    As an aside, you might have noticed that neither your config file data nor your output render well. This is most likely because you did not wrap it in <code>...</code> tags, which make code and data render nice on this site.

      I think you have missed one key. Give the input correctly. Use the code Tag. Give some input and output.
Re: How do I make all the keys in %a with common keys in %b equal to %b's values?
by almut (Canon) on Mar 24, 2010 at 07:42 UTC
    for (keys %a) { $a{$_} = $b{$_} if exists $b{$_}; } # or for (keys %b) { $a{$_} = $b{$_} if exists $a{$_}; } # or $a{$_} = $b{$_} for grep { exists $b{$_} } keys %a; # or $a{$_} = $b{$_} for grep { exists $a{$_} } keys %b;

    (whether to iterate over keys %a or keys %b is essentially arbitrary — pick the one with fewer keys (if known beforehand) )

Re: How do I make all the keys in %a with common keys in %b equal to %b's values?
by salva (Canon) on Mar 24, 2010 at 08:13 UTC
    my @common = grep exists $a{$_}, keys %b; @a{@common} = @b{@common};
Re: How do I make all the keys in %a with common keys in %b equal to %b's values?
by shmem (Chancellor) on Mar 24, 2010 at 07:52 UTC
    my @k = grep { exists $b{$_} } keys %a; @a{@k} = @b{@k};