my %lookup_row; foreach my $row ( @RAM ) { my $hash = hash($row); if ( defined $lookup_row{$hash} ) { push @{ $lookup_row{$hash} }, $row; } else { $lookup_row{ hash($row) } = [$row]; } } foreach my $row ( @RAM2 ) { if ( exists $lookup_row{ hash($row) } ) { # potential match # check for collision } } sub hash { # something that makes sense for your data # probably join in this case. }