in reply to Merging Two Strings
The regular expression s/(.+)-\1/$1/ will solve it almost. It won't satisfy (yet) your "last-match" rule
(first attempt in readmore)#!/usr/bin/perl use strict; use warnings; my %exampledata= ( ATTTA => 'TTTAA', ATGTA => 'ATGTA', ATGATG => 'ATGATG', ); while (my($s1, $s2)= each %exampledata) { print $s1,'+',$s2,' => ',merge_data($s1, $s2),"\n"; }
sub merge_data { local($_)= join("-", @_); s/(.+)-\1/$1/; return $_; }
Update: This will take care of the rule (in readmore)
sub merge_data { local($_)= join("-", @_); my($m_str)= $_; if (s/(.+)-\1/$1/) { my $first_result= $_; my $chop_off= length($_[0])-length($1)+1; $_= substr($m_str, $chop_off); if (s/(.+)-\1/$1/) { return substr($m_str, 0, $chop_off) . $_; } return $first_result; } return $_; }
Update: This will take care of example 2 by inserting an arbitrary rule.
Rules are now: Find all matches and take a) the second best match that's longer than 1 character b) the best match
sub merge_data { local($_)= join("-", @_); my($m_str)= $_; if (s/(.+)-\1/$1/) { my $first_result= $_; my $chop_off= length($_[0])-length($1)+1; $_= substr($m_str, $chop_off); if (s/(.{2,})-\1/$1/) { return substr($m_str, 0, $chop_off) . $_; } return $first_result; } return $_; }
|
|---|