It might be easier for you to use just strings rather than the overhead of arrays for this. Use the substr function to get the nth character in the sequence.
my $dna1 = 'CACTATGAGTGATCGC'; my $dna2 = 'ACTGACTAATGCGTTG'; print "$dna1\n$dna2\n"; for (0 .. length($dna1)-1) { if (substr($dna1,$_,1) eq substr($dna2,$_,1)) { print ' '; } else { print '*'; } } print "\n"; __END__ # prints this: CACTATGAGTGATCGC ACTGACTAATGCGTTG **** ** * *****
Also, there are quite a few problems in your original code. First, the value that you want to loop/iterate over is the number N when you are looking at the Nth character. But the foreach loop will set $base to each of the values in @lines1 and then to all the values in @lines2. You cannot get a pair of elements from two different arrays using a foreach loop. Also note that inside the loop, you are comparing the first elements of the array each time. You want to compare a different pair of elements each time through the loop. Lastly, @lines1[0] is invalid (at least until Perl6), and what you need to say instead is $lines1[0].

HTH and keep trying!

blokhead


In reply to Re: comparing strings by blokhead
in thread comparing strings 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.