Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.

In reply to Re: Request to correct the perl code for getting substrings by oldtomas
in thread Request to correct the perl code for getting substrings by supriyoch_2008

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (5)
    As of 2013-05-19 05:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best material for plates (tableware) is:









      Results (397 votes), past polls