Guys,
I want to use bitwise XOR operator which is known to be fast for bitwise testing. What I intend to do below is to find number of matching positions (except N matching is not counted).
my $s1 = "STCATTNNNSATCGCT"; my $s2 = "ATCGTCGSNNNNATCG"; ## # 0 # # There are 4 matching positions - marked with (#) - # and notice that matching N is not counted (0)
My best attempt with it is this, which leads me nowhere.
my $match = $s1 ^ $s2; my $match_count = $match =~ tr/\00//; print "$match_count\n"; # This gives 5, because N is counted. # How can I make it to return 4 instead?
Because the code I have below - using subscripted for loop - already does its job, but it is horribly slow:
get_match_count($s1,$s2); sub get_match_count { my ( $k, $l ) = @_; return 0 if $k eq $l; my $len = length($k); my $num_match = 0; for ( my $i = 0; $i < $len; $i++ ) { next if ( substr( $k, $i, 1 ) eq 'N' and substr( $l, $i, 1 ) eq 'N' ); ++$num_match if substr( $k, $i, 1 ) eq substr( $l, $i, 1 ); } print "$num_match\n"; return $num_match; }

Regards,
Edward

In reply to Conditional Bitwise XOR(^) Operator in Matching Chars Count of Two Strings? by monkfan

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.