Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > elegant way to do the union of 2 strings

Reply
Thread Tools

elegant way to do the union of 2 strings

 
 
avilella
Guest
Posts: n/a
 
      09-21-2009
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";

Anyone? Maybe using a regular expression?

Thanks,

Albert.
 
Reply With Quote
 
 
 
 
Danny Woods
Guest
Posts: n/a
 
      09-21-2009
avilella <> writes:

> 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";


Perhaps:

$seq1 =~ s/-/substr($seq2, pos($seq1), 1)/eg;

Works for your test case. Perhaps you have more data to test it
against?

Cheers,
Danny.
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-21-2009
avilella <> wrote:
>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";
>
>Anyone? Maybe using a regular expression?


Sometimes just using different terminology solves the mysterie:

In sequence 1 (logically those are not strings but as you indicated
yourself they are sequences) you want to replace each '---' element with
the corresponding element from sequence 2.

#untested, sketch only
my @a = split(/ /, $seq1);
my @b = split(/ /,$seq2);
for my $i (0..$#a) {
if ($a[i] eq '---') {$a[i] = $b[i]};
}
my $res = join (' ', @a);

This can probably be optimized somewhat by using map() and computing the
result elements in the argument of the join() but this way the logic is
very accessible.

jue
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      09-21-2009
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);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Indenting strings - elegant alternatives? Ronald Fischer Ruby 7 09-20-2007 08:46 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
In search of elegant code - do two strings differ by one character? usenet@DavidFilmer.com Perl Misc 2 08-01-2005 12:04 PM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57