I am trying to do Merging in perl where array which contains hashes in it, I am trying to not merge duplicate elements(via uniq function) in an array while merging.
use strict;
use Hash::Merge qw/merge/;
use List::MoreUtils qw/uniq/;
use Data::Dumper;
my $h1 = { name1 => [ {
sport => "tennis",
country => "us",
},
{
sport => "soccer",
country => "brazil",
},
{
sport => "cricket",
country => "india"
},
]
};
my $h2 = { name1 => [ {
sport => "soccer",
country => "brazil",
},
{
sport => "boxing",
country => "china"
}
],
};
Output:-
$VAR1 = {
'name1' => [
{
'country' => 'us',
'sport' => 'tennis'
},
{
'country' => 'brazil',
'sport' => 'soccer'
},
{
'sport' => 'cricket',
'country' => 'india'
},
{
'country' => 'brazil',
'sport' => 'soccer'
},
{
'country' => 'china',
'sport' => 'boxing'
}
]
};
When I try to merge with custom merge method, I am not able to eliminate duplicate elements ({ sport => "soccer" country => "brazil" }). Seems like reason is because of elements in hash format. Do we have any easy way using custom merge technique to achieve it. Thanks!