shan_emails has asked for the wisdom of the Perl Monks concerning the following question:
$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, $reference) = @_; my %output = %$ai; my %ref = %$reference; 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}{$filename} +; } } } } return \%output; }
$h3 = { 'drug comparison' => { '7003000.xml' => { 'entity' => 'a1, a2, a3' }, '70037559.xml' => { 'entity' => 'x1, x2, x3' }, '7004562.xml' => { 'entity' => 'z1, z2, z3' } } };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Generate union of the two hashes
by kennethk (Abbot) on Sep 08, 2010 at 14:07 UTC | |
by shan_emails (Beadle) on Sep 08, 2010 at 15:19 UTC | |
|
Re: Generate union of the two hashes
by CountZero (Bishop) on Sep 08, 2010 at 13:44 UTC | |
|
Re: Generate union of the two hashes
by MajingaZ (Beadle) on Sep 08, 2010 at 14:41 UTC |