good chemistry is complicated,and a little bit messy -LW PerlMonks

### How to get the correct count

by supriyoch_2008 (Monk)
 on Apr 19, 2012 at 10:48 UTC Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Dear PerlMonks,

I am a beginner in perl programming. I am getting wrong result in counting a regular expression from my program. I have given the program below. Can you help me to sort out this problem?

```#!usr/bin/perl-w
@k = ‘CCATGNNNTAACCNNATGNNTAGCC’;
\$k = join( '', @k);
# Remove whitespace
\$k=~ s/\s//g;
# Count number of bases & regular expressions
\$b=length(\$k);
print "\n Number of bases (including N): \$b.\n";
# Count number of A and regular expression (i.e. gene)
\$A=0; \$GENE=0;
while(\$k=~ /A/ig){\$A++}
while(\$k=~ /[AG]TG.*T[AG][AG]/ig){\$GENE++}
print "\n A= \$A; GENE(S)= \$GENE; \n\n";
exit;

I have got the following wrong result:

Number of bases (including N): 25.

A= 5; GENE(S)= 1;

But correct result should be:

Number of bases (including N): 25.

A= 5; GENE(S)= 2;

Actually the value of GENE(S) should be 2 as there are two such regular expressions (sequences) in the array @DNA. Please suggest me the corrections in the code.

Replies are listed 'Best First'.
Re: How to get the correct count
by GrandFather (Sage) on Apr 19, 2012 at 11:13 UTC

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

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;
to:
```\$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;
to:
```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;

Re: How to get the correct count
by Ratazong (Monsignor) on Apr 19, 2012 at 10:59 UTC

```while(\$k=~ /[AG]TG.*T[AG][AG]/ig){\$GENE++}
This is probably not doing what you expect, because the quantifier * is greedy. You might change it to
```while(\$k=~ /([AG]TG.*T[AG][AG])/ig){\$GENE++; print "\$1\n";}
to see what happens. (The new code stores the matching part (using () ) to the special variable \$1 and prints it).

HTH, Rata

Create A New User
Node Status?
node history
Node Type: perlquestion [id://965908]
Approved by GrandFather
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2017-08-21 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (322 votes). Check out past polls.

Notices?