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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|