use strict; my @a = qw(fred bob joe jim ethfgh mary elaine foo bar bob foo too); my @b = qw(frank joe jim dfkjhgdkjfg mary bob srere dfhgerg wet sdfwer mary); my (%seenA, %seenB); { my $i = 0; @seenA{@a}=undef; @seenB{@b}=undef; my $last = -1; @a = map {exists $seenB{$_} ? ($last = $_) : (defined($last) ? ($last = undef) : ())} @a; my $last = -1; @b = map {exists $seenA{$_} ? ($last = $_) : (defined($last) ? ($last = undef) : ())} @b; } print "@a\n"; print "@b\n\n"; shift(@a) unless defined $a[0]; shift(@b) unless defined $b[0]; pop(@a) unless defined $a[-1]; pop(@b) unless defined $b[-1]; print "@a\n"; print "@b\n\n"; my @result; while (@a and @a > @result) { my $start = 0; while (@b - $start > @result) { my $end = 0; my @maybe = (); $end++ while (defined($a[$end]) and defined($b[$start+$end]) and $a[$end] eq $b[$start+$end]); if (--$end > @result) { @result = @a[0..$end]; } $start++; } shift(@a); } print "@result\n";