Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: How to get the correct count

by GrandFather (Sage)
on Apr 19, 2012 at 11:13 UTC ( #965915=note: print w/replies, xml ) Need Help??

in reply to How to get the correct count

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


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

Replies are listed 'Best First'.
Re^2: How to get the correct count
by eyepopslikeamosquito (Chancellor) on Apr 19, 2012 at 11:48 UTC

    I endorse all the excellent improvements made by GrandFather. Some further minor tweaks follow. For removing whitespace, you could change from:

    $seq =~ s/\s//g;
    $seq =~ s/\s+//g;
    That should be a touch more efficient and is more idiomatic -- see also perlfaq4 "How do I strip blank space from the beginning/end of a string?".

    An alternative for counting the number of bases is to change from:

    my $A = () = $seq =~ /A/ig;
    my $A = $seq =~ tr/aA//;
    Counting characters with tr seems a bit odd but is normally fastest and is idiomatic Perl -- see Quote and Quote-like Operators.

    Finally, to see what actually matches in the regex, and so help you debug it, note that you could expand it a bit like this:

    my @matches = $seq =~ /[AG]TG.*?T[AG][AG]/ig; print "Here are the matched parts of the string:\n"; print " '$_'\n" for @matches; my $GENE = @matches;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://965915]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-06-22 00:28 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (120 votes). Check out past polls.