1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
use strict;
use warnings;
use Data::Dumper;
my $ancien = {
maison => [1,7],
chien => [15,50]
};
my $nouveau = {
lunette => [4,8],
paris => [12,5],
chien => [13, 15,50]
};
print Dumper traitement($ancien, $nouveau);
sub traitement {
my ($ancien, $nouveau) = @_;
my $result = {};
while ( my ($k, $v) = each %$nouveau ) {
if (my $old_v = $ancien->{$k}) {
# key exists, do we have new values?
if (my @result_v = _new_tab($old_v, $v)) {
# new values
$result->{$k} = \@result_v
}
} else {
# new key, keep all values
$result->{$k} = $v;
}
}
return $result;
}
sub _new_tab {
my ($ancien, $nouveau) = @_;
# [12, 15, 50], [13, 15, 50] => (13)
# [15, 50], [15, 50] => ()
my %seen = map {$_ => 1} @$ancien;
return grep { not $seen{$_} } @$nouveau;
} |
Partager