#! perl -slw use strict; sub lcs_arrays1 { my ( $a1, $a2 ) = @_; my @matches; my $longest = 0; my @last; my @this; for my $b1 ( 0 .. $#$a1 ) { @this = @last; @last = (); for my $b2 ( 0 .. $#$a2 ) { if ( $$a1[$b1] eq $$a2[$b2] ) { my @match = ( $$a1[$b1] ); my $e2 = $b2+1; $this[$e2] = $last[$e2-1] + 1; if ($this[$e2] > $longest) { $longest = $this[$e2]; @matches = (); } if ($this[$e2] == $longest) { push @matches, [ @{$a1}[ ($b1-$longest+1), $b1 ] ]; } } } } return $matches[0]; # array ref -> longest matching list } sub lcs_arrays2 { my ( $a1, $a2 ) = @_; my @match; my $longest = 0; my @last; my @this; for my $b1 ( 0 .. $#$a1 ) { @this = @last; @last = (); for my $b2 ( 0 .. $#$a2 ) { if ( $$a1[$b1] eq $$a2[$b2] ) { my @match = ( $$a1[$b1] ); my $e2 = $b2+1; $this[$e2] = $last[$e2-1] + 1; if ($this[$e2] > $longest) { $longest = $this[$e2]; @match = @{$a1}[ ($b1-$longest+1), $b1 ]; } } } } return \@match; # array ref -> longest matching list } my @a = ( 'a'..'d' ); my @b = ( 'b'..'d' ); print 'v1: ', @{ lcs_arrays1( \@a, \@b ) }; print 'v2: ', @{ lcs_arrays2( \@a, \@b ) }; __END__ C:\test\690326>ikegami.pl Use of uninitialized value in addition (+) at C:\test\690326\ikegami.pl line 20. Use of uninitialized value in addition (+) at C:\test\690326\ikegami.pl line 20. Use of uninitialized value in addition (+) at C:\test\690326\ikegami.pl line 20. v1: bb Use of uninitialized value in addition (+) at C:\test\690326\ikegami.pl line 55. Use of uninitialized value in addition (+) at C:\test\690326\ikegami.pl line 55. Use of uninitialized value in addition (+) at C:\test\690326\ikegami.pl line 55. v2: