Also if the lists are huge and a lot of the items only appear on one of the lists it may be better to filter out those "unique" elements before you start looking for matches. Of course you can't remove them completely, you have to keep a "marker" there that's not equal to any other element nor marker.
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) ? ($l
+ast = undef) : ())} @a;
my $last = -1;
@b = map {exists $seenA{$_} ? ($last = $_) : (defined($last) ? ($l
+ast = 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]) a
+nd $a[$end] eq $b[$start+$end]);
if (--$end > @result) {
@result = @a[0..$end];
}
$start++;
}
shift(@a);
}
print "@result\n";
I assume the lists did not contain any undefs!
Jenda
Always code as if the guy who ends up maintaining your code
will be a violent psychopath who knows where you live.
-- Rick Osborne
Edit by castaway: Closed small tag in signature
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|