Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

The immediate problem is that * is greedy - it matches as much as it can so it gobbles up everything from the start of the first gene to the end of the last gene. The solution is to use the ? modifier to make it non-greedy: /[AG]TG.*?T[AG][AG]/ig. However there is room for a little more tidying up of your script. Consider:

use strict; use warnings; my $seq = 'CCATGNNNTAACCNNATGNNTAGCC'; $seq =~ s/\s//g; # Count number of bases & regular expressions my $bases = length ($seq); my $A = () = $seq =~ /A/ig; my $GENE = () = $seq =~ /[AG]TG.*?T[AG][AG]/ig; print <<REPORT; Number of bases (including N): $bases. A= $A; GENE(S)= $GENE; REPORT

Prints:

Number of bases (including N): 25. A= 5; GENE(S)= 2;

Note that you should always use strictures (use strict; use warnings;) to catch various errors and typos as soon as possible. Avoid using $a and $b because they are special variables used by sort.

The array is not needed at all as you only assign a single value to it, but in any case you should not use the same name for different variables (the array @k and the scalar $k are different variables) because you will very likely cause confusion that way - confusing scripts are hard to maintain.

The = () = trick puts the regular expression in list context and allows the assignment to effectively directly assign the count of matches to a scalar.

The <<REPORT in the print introduces a here doc. The following lines up to the matching REPORT behave as a string that effectively replaces <<REPORT.

True laziness is hard work

In reply to Re: How to get the correct count by GrandFather
in thread How to get the correct count 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: (9)
    As of 2014-09-18 11:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (111 votes), past polls