in reply to Re: Generate union of the two hashes
in thread Generate union of the two hashes
use Clone qw(clone); $h1 = { 'drug comparison' => { '7003000.xml' => { 'entity' => 'a1, a2, a3' }, '70037559.xml' => { 'entity' => 'x1, x2, x3' } } }; $h2 = { 'drug comparison' => { '7004562.xml' => { 'entity' => 'z1, z2, z3' }, '70037559.xml' => { 'entity' => 'e1, e2, e3' } } }; $h3 = union_of_h1_h2 ($h1, $h2); print $h3; sub union_of_h1_h2 { my ($ai, $ref) = @_; my $output = clone($ai); foreach my $_ref (keys %$ref) { unless (exists $output->{$_ref}) { $output->{$_ref} = $ref->{$_ref}; }else { foreach my $filename (keys %{$ref->{$_ref}}) { if (!exists $output->{$_ref}->{$filename}) { $output->{$_ref}->{$filename} = $ref->{$_ref}->{$f +ilename}; } } } } return $output; }
|
|---|