in reply to Merging Two Strings
This seems to do what you want:
use warnings; use strict; while (<DATA>) { chomp; my $first = $_; my $second = <DATA>; last if ! defined $second; chomp $second; (print "Degenerate case: $first, $second\n"), next if length ($first) == 1 or length ($second) == 1; my $overlap = 0; while ($overlap < length $first) { my $tail = substr $first, -$overlap - 1; my $match = $tail ^ substr $second, 0, $overlap + 1; last if $overlap > 1 && $match =~ /^\0+$/; ++$overlap; } my $result = $first . substr $second, $overlap + 1; print "Merge $first and $second as $result\n"; } __DATA__ A ATG ATTTA TTTAA ATGTA ATGTA ATGATG ATGATG
Prints:
Update for sauoq's test caseMerge ATTTA and TTTAA as ATTTAA Merge ATGTA and ATGTA as ATGTA Merge ATGATG and ATGATG as ATGATGATG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Merging Two Strings
by sauoq (Abbot) on Oct 27, 2005 at 10:47 UTC | |
by GrandFather (Saint) on Oct 27, 2005 at 18:32 UTC |