elegant way to do the union of 2 strings

Discussion in 'Perl Misc' started by avilella, Sep 21, 2009.

  1. avilella

    avilella Guest

    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.
     
    avilella, Sep 21, 2009
    #1
    1. Advertising

  2. avilella

    Danny Woods Guest

    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.
     
    Danny Woods, Sep 21, 2009
    #2
    1. Advertising

  3. 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 eq '---') {$a = $b};
    }
    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
     
    Jürgen Exner, Sep 21, 2009
    #3
  4. avilella

    ccc31807 Guest

    On Sep 21, 9:12 am, avilella <> 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);
     
    ccc31807, Sep 21, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    669
    Matt Garman
    Apr 25, 2004
  2. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    875
    Chris Torek
    Apr 26, 2004
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  4. Ronald Fischer
    Replies:
    7
    Views:
    120
    Bertram Scharpf
    Sep 20, 2007
  5. Replies:
    2
    Views:
    118
    Anno Siegel
    Aug 1, 2005
Loading...

Share This Page