First, you are not comparing strings, you are comparing arrays.

As this is not C, you don't need a for loop to get the length of an array: just say:

$counter=@dna;$counter2=@comp;
Assigning an array to a scalar assigns the length of the array to the scalar (OK, we're talking about context here, but I'm not going into a detailed discussion)

And your cycles are a bit out of whack... you are saying:

  1. if @dna is not empty (while (@dna))
  2. count how many bases I have in @dna
  3. count how many bases I have in @comp
  4. check if the one past last element of each list is a correct pairing
  5. etc

I'll not write here a "correct" solution, since many other people already have. I'll just write a (commented) snippet for the case of strings:

my $dna; # the sequence, as a string my $comp; # the complement, as a string my $c2; ($c2=$comp)=~tr[ATGC][TACG]; # copy $comp into $c2, then exchange each + base in $c2 with its complement (using the tr-ansliteration operator +) print "Match!\n" if $dna eq $c2; # since $c2 is $comp's complement, it + should be equal to $dna

-- 
        dakkar - Mobilis in mobile

In reply to Re: need help comparing DNA compliments by dakkar
in thread need help comparing DNA compliments by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.