#!/usr/bin/perl -w use strict; my @array1 = qw[a b c d e]; my @array2 = qw[t e s t d a t a]; my @sort1 = sort @array1; my @sort2 = sort @array2; while (@sort1 && @sort2) { if ($sort1[0] eq $sort2[0]) { my $match = $sort1[0]; while (@sort1 && $sort1[0] eq $match) { shift @sort1; } while (@sort2 && $sort2[0] eq $match) { shift @sort2; } print "$match is in both arrays\n"; } elsif ($sort1[0] lt $sort2[0]) { shift @sort1; } else { shift @sort2; } }