The OP seems to want to match at the start of the target string with either

The code of Re: string match using with an N in any position matches 'CACGT' against 'CBCGTNNN' ('B' vice 'N').

>perl -wMstrict -le "my @queries = qw[ GCGAT CACGT ]; ;; my @targets = qw(GNGATNNN GCGANBBB CNCGTNNN CBCGTNNN ); ;; for my $q (@queries) { for my $t (@targets) { my $matched = ($q ^ substr($t, 0, length $q)) =~ tr[\0][\0]; if($matched >= (length($q) - 1)) { print qq{'$q' matched '$t'}; } } } " 'GCGAT' matched 'GNGATNNN' 'GCGAT' matched 'GCGANBBB' 'CACGT' matched 'CNCGTNNN' 'CACGT' matched 'CBCGTNNN'

Here's a variation that avoids this (although the conditional logic is a bit obscure).

>perl -wMstrict -le "use List::MoreUtils qw(uniq); ;; my @queries = qw(GCGAT CACGTT); ;; my $n_diff = join '', uniq map { sprintf '\x%02x', ord($_ ^ 'N') } map { split // } @queries ; $n_diff = eval qq{ sub { return \$_[0] =~ tr/$n_diff/$n_diff/; } }; ;; my @targets = qw( GNGATNNNHIT GCGANBBBHIT CNCGTTNNNHIT CACGTTNNNHIT CBCGTTNNNMISS CNNGTTNNNMISS NCACGTTNNNMISS ); ;; for my $q (@queries) { my $len_q = length $q; TARGET: for my $t (@targets) { my $mask = $q ^ substr $t, 0, $len_q; my $nulls = $mask =~ tr{\0}{\0}; next TARGET if $len_q > $nulls + 1 or $len_q > $nulls && $n_diff->($mask) != 1 ; print qq{'$q' matched '$t'}; } } " 'GCGAT' matched 'GNGATNNNHIT' 'GCGAT' matched 'GCGANBBBHIT' 'CACGTT' matched 'CNCGTTNNNHIT' 'CACGTT' matched 'CACGTTNNNHIT'

In reply to Re^2: string match using with an N in any position by AnomalousMonk
in thread string match using with an N in any position by biobee07

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.