Hi monks,

I have a pattern that I'm looking to do a search and replace on, but I am finding that the S&R is only occurring about 2/3 of the time. I looked around a bit, and found this in the perlop page:

Occasionally, you can't use just a /g to get all the changes to occur. + Here are two common cases: # put commas in the right places in an integer 1 while s/(.*\d)(\d\d\d)/$1,$2/g; # perl4 1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g; # perl5 # expand tabs to 8-column spacing 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
Now, the PATTERN I've been searching for is a complicated one. I was having trouble setting it up initially, and asked for help here and here. The solution I eventually wound up using was a modification of the one provided by johngg, seen here. The pertinent bits of the code I'm using are pasted below (use strict and warnings are both in use, all variables are declared, etc. This is just a snippet)
# patterns to search with are stored in @pattern Array # patterns are strings of uc DNA (i.e. TTTAGT, etc) # but can be interrupted by lc or non-DNA characters (i.e., html tags) # searching for a needle in a haystack: my $notNeedle = q{[^ACGTYRMKSWBDHVN]*}; while ($g < $num) { $needle[$g] = $pattern[$g]; @{$parts[$g]} = split m/(\[|\]|\|)/, $needle[$g]; my @array = @{$parts[$g]}; my @results; while (@array) { my $string = shift @array; if ($string eq '[') { $string .= (join "", splice(@array, 0, 2)); } if ($string eq '|') { my $prev = pop @results; $string = (join "", $prev, $string, splice(@array, 0, 1)); } push @results, $string; } @{$parts[$g]} = @results; $haystackPatt[$g] = q{(} . join($notNeedle, @{$parts[$g]}) . q{)}; $rxHaystack[$g] = qr{$haystackPatt[$g]}; $g++; } $text_seqs[$i] =~ s{$rxHaystack[$j]}{<span class=$class[$a]>$1</span>} +gemx;
The problem is, if I search for something where $pattern[$i] = "TTT|AAA", I will get only about 2 out of every three AAAs or TTTs flanked by the <span> tags.

Does anyone have any ideas why this isn't replacing all of the PATTERNS? Any help, ideas, or links pointing to possible explanations would be greatly appreciated.

Thanks
Matt


In reply to s/// only replacing most of the time... by mdunnbass

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.