0

After this question, I've encountered the following problem:

I'm trying to merge two hashes that hold lists inside them. The thing is that those lists are exactly the same, but because they are lists, the merger duplicates their values inside. I've edited according to the answer from the previous question, but I'm stuck with this issue.

Any ideas how can I remove the duplication?

The following example doesn't work:

#!usr/bin/perl
use strict;
use warnings;
use Hash::Merge;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;



my $hash1 = {
            'EndHeaderExists' => [
{                                                                      
  'i' => 1
}
                                 ]} ;
my $hash2 = {
    'EndHeaderExists' => [
{
'i' => 1
}
]} ;


my $merger = Hash::Merge->new('LEFT_PRECEDENT');    
my $behavior = $merger->get_behavior_spec($merger->get_behavior);


$behavior->{ARRAY}{ARRAY} = sub {
    my ($left, $right) = @_;
    my %seen = map { $_ => 1 } @$left;
    print Dumper(\%seen);
    return [ @$left, grep { ! $seen{$_} } @$right ];
};
my $hash3 = $merger->merge($hash2, $hash1);
print Dumper($hash3);

Output:

$VAR1 = {                     # %Seen output 
          'HASH(0xdd1af8)' => 1
        };


$VAR1 = {                           # The real output
          'EndHeaderExists' => [
                                 {
                                   'i' => 1
                                 },
                                 {
                                   'i' => 1
                                 }
                               ]
        };

Expected output:

$VAR1 = { 
'EndHeaderExists' => [
                                 {
                                   'i' => 1
                                 }
]}
urie
  • 361
  • 2
  • 14

0 Answers0