On Sep 21, 9:12*am, avilella <avile...@gmail.com> wrote:
> Hi,
>
> I am looking for an elegant way to do the union of two string. For
> example:
>
> $seq1 = "--- --- GAA --- GGA";
> $seq2 = "AAC TGG --- --- ---";
>
> The rule is to do the union of the letters, leaving the dash symbol
> when both have a dash in a given position:
>
> $union = "AAC TGG GAA --- GGA";
I would approach this from the standpoint of comparing arrays rather
than looking at strings. Split each sequence and then iterate through
the arrays.
use strict;
use warnings;
my $s1 = "--- --- GAA --- GGA";
my $s2 = "AAC TGG --- --- ---";
my @s1 = split / /, $s1;
my @s2 = split / /, $s2;
my @s3 = ();
my $len = @s1; #get the length
for (my $i = 0; $i < $len; $i++)
{
$s3[$i] = '---' if $s1[$i] eq '---' and $s2[$i] eq '---';
$s3[$i] = $s1[$i] if $s1[$i] =~ /[A-Z]{3}/ and $s2[$i] eq '---';
$s3[$i] = $s2[$i] if $s1[$i] eq '---' and $s2[$i] =~ /[A-Z]{3}/;
$s3[$i] = '***' if $s1[$i] =~ /[A-Z]{3}/ and $s2[$i] =~ /[A-Z]{3}/;
}
my $s3 = join ' ', @s3;
print $s3;
exit(0);
|