Hi,
I don't know why you are assigning first to an array, like so:
@d = “CCATGNNNTAACCNNATGNNTAGCC”;
(this assigns the scalar value "CCATG..." to $d[0])
just to flatten it later like so:
my $string ="@d";
There are a couple of other things in your code which are unclear to me. That said, the essential bits might be done this way:
#!/usr/bin/perl -w
my $string = "CCATGNNNTAACCNNATGNNTAGCC";
while($string =~ /[AG]TG.*?[AG][AG]/g) {
print "matched at ", pos($string) - length($&), ": ", $&, "\n";
}
Notes:
- The /g flag at the end of the match operator makes the match "global". In scalar context (that's given within the while() parentheses) it makes that the match is retried each time where it left off the last time, until no more matches are left.
- Once a match is found, the end position of the match is available in pos($string). The match itself is available in the special variable $&
- I flagged the "any" part in the regexp (i.e. the ".*") with a non-greedy modifier (that makes ".*?"). This will try the shortest possible matches. If that's not what you want, leave the "?" out.
-
- This won't find overlapping matches. You could achieve that resetting pos($string), e.g. to pos($string) - length($&) + 1 or so.
I hope I understood your problem statement correctly; I'm a bit confused by your sample code, though.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.