#!/usr/bin/perl use strict; use warnings; use Data::Dumper qw/Dumper/; my @pairs_in_db = ([1,2],[1,9],[3,4],[5,6]); my @pairs_from_somewhere_else = ([1,2],[5,6],[7,18]); my %map = (); foreach my $pair ( @pairs_in_db ) { $map{$pair->[0]}->{$pair->[1]} = 1; } my %keep = (); foreach my $pair ( @pairs_from_somewhere_else ) { if( exists $map{$pair->[0]} && exists $map{$pair->[0]}->{$pair->[1]} ) { printf("<%s, %s> was in db\n", @{$pair}); $keep{$pair->[0]}->{$pair->[1]} = 1; }else{ printf("<%s, %s> has to be inserted into the databse\n", @{$pair}); } } foreach my $pair ( @pairs_in_db ) { unless( exists $keep{$pair->[0]} && exists $keep{$pair->[0]}->{$pair->[1]} ) { printf("<%s, %s> will be removed\n", @{$pair}); } }