in reply to Merging two hashes, keeping keys unique but adding values if necessary

I still can't see an error in your approach, but simply adding to an empty hash looks simpler, is easier to maintain and likely more time efficient. :)

DB<103> %copyhash=( key1 => '1', key15 => '1', key140 => '1', ); => ("key1", 1, "key15", 1, "key140", 1) DB<104> %muthash = ( key1 => '1', key15 => '3', key150 => '1', ); => ("key1", 1, "key15", 3, "key150", 1) DB<105> $mergedhash{$_} += $muthash{$_} for keys %muthash => "" DB<106> $mergedhash{$_} += $copyhash{$_} for keys %copyhash => "" DB<107> \%mergedhash => { key1 => 2, key140 => 1, key15 => 4, key150 => 1 }

Cheers Rolf

(addicted to the Perl Programming Language)

  • Comment on Re: Merging two hashes, keeping keys unique but adding values if necessary
  • Download Code