#!/usr/bin/perl use strict; use warnings; sub pair_hash { return $_[0][0] . ',' . $_[0][1]; } sub array_union { my ($a, $b) = @_; my %bhash = map { &pair_hash($_) => 1 } @{$b}; return grep { defined $bhash{ &pair_hash($_) } } @{$a}; } sub array_diff { my ($a, $b) = @_; my %bhash = map { &pair_hash($_) => 1 } @{$b}; return grep { !defined $bhash{ &pair_hash($_) } } @{$a}; } my @pairs_in_db = ([1,2],[1,9],[3,4],[5,6]); my @pairs_from_somewhere_else = ([1,2],[5,6],[7,18]); my @was_in_db = &array_union(\@pairs_in_db, \@pairs_from_somewhere_else); my @pairs_to_delete = &array_diff(\@pairs_in_db, \@pairs_from_somewhere_else); my @pairs_to_create = &array_diff(\@pairs_from_somewhere_else, \@pairs_in_db); foreach my $ext (@was_in_db) { printf("<%s, %s> was in db\n", @{$ext}); } foreach my $ins (@pairs_to_create) { printf("<%s, %s> has to be inserted into the databse\n", @{$ins}); } foreach my $del (@pairs_to_delete) { printf "<%s, %s> will be removed\n", @{$del}; }