Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

How to get the correct count

by supriyoch_2008 (Monk)
on Apr 19, 2012 at 10:48 UTC ( #965908=perlquestion: print w/replies, xml ) 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


    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;
      $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;

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965908]
Approved by GrandFather
[LanX]: Jeez .... UTF8 in my company is driving me crazy
[LanX]: seems like my boss has activated an extra UTF8 encoding such that my JSON stuff arives twice encoded in the browser ... oO
[LanX]: he loves to do this with regexes ...
LanX considers looking for a new project ...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2018-03-19 23:22 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.