in reply to Merge 2 array/Hash into 1 variable Perl
G'day kris1511,
Welcome to the Monastery.
I'm guessing that you'd only want unique values in your final hash (i.e. you wouldn't want something like "status => [124, 124, 137]"). Here's a way to do what you ask and exclude duplicate values.
#!/usr/bin/env perl use strict; use warnings; use Data::Dump; my $h1 = {locset => [409], status => [501, 137]}; my $h2 = {status => [137, 124], locset => [405, 409], class => [0]}; my %seen = map { $_ => { map { $_ => 1 } @{$h1->{$_}} } } keys %$h1; my $x = $h1; for my $key (keys %$h2) { if (exists $seen{$key}) { push @{$x->{$key}}, grep { ! $seen{$key}{$_} } @{$h2->{$key}}; } else { $x->{$key} = $h2->{$key}; } } dd $x;
Output:
{ class => [0], locset => [409, 405], status => [501, 137, 124] }
— Ken
|
|---|