Search code examples
perlperl-data-structures

Merge Hashes in Perl: Special Case


How to merge two hashes in perl where keys can collide and the values are arrays.? In case of collision I want to merge the value arrays.

Would normal merge be just fine?

I am sorry if this is a repetition, but I tried looking up but nothing such specific turned up.

Thanks!


Solution

  • To merge %hoa2 into %hoa1:

    for (keys(%hoa2)) {
       push @{ $hoa1{$_} }, @{ $hoa2{$_} };
    }