use strict; my @a = qw( fred bob joe jim mary elaine ); my @b = qw( frank joe jim mary bob ); my @result; while (@a and @a > @result) { my $start = 0; while (@b - $start > @result) { my $end = 0; my @maybe = (); $end++ while (exists($a[$end]) and $a[$end] eq $b[$start+$end]); if (--$end > @result) { @result = @a[0..$end]; } $start++; } shift(@a); } print "@result\n";