Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Check even numbers?

by Anonymous Monk
on Aug 06, 2006 at 22:07 UTC ( [id://565869]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello there monks!
I have a counter inside a Perl script, which counts the number of occurences of a certain pattern in a file.
The lines in the file are something like:
>protein1
sequence1
>protein2
sequence2
>protein3
sequence3
etc
So, if, in each sequence, the program finds, for instance, the letter 'A', it adds 1 to the counter. What I want to print out is those proteins and their sequences, that have even number of occurences of the letter 'A' (2,4,6,8 etc).
Is there a function or something that checks if a number is odd or even?
Thank you!

Replies are listed 'Best First'.
Re: Check even numbers?
by jeffa (Bishop) on Aug 06, 2006 at 22:10 UTC

    You can use the modulus operator for that.

    my $is_even = $number % 2 == 0; my $is_odd = $number % 2 == 1;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Check even numbers?
by GrandFather (Saint) on Aug 06, 2006 at 22:12 UTC

    Check the least significant bit. It is set for odd numbers:

    print "$_ is " . ($_ & 1 ? "odd" : "even") . "\n" for 0..3;

    Prints:

    0 is even 1 is odd 2 is even 3 is odd

    DWIM is Perl's answer to Gödel
Re: Check even numbers?
by liverpole (Monsignor) on Aug 06, 2006 at 22:12 UTC
    A very simple method for checking whether a number $N is odd or even is to ask yourself "is there any remainder when I divide $N by 2?".

    So, for example:

    if (0 == $N % 2) { print "The number $N is even\n"; # There is no remainder } else { print "The number $N is odd\n"; # There is a remainder (of 1) }

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Check even numbers?
by cees (Curate) on Aug 07, 2006 at 05:02 UTC

    If you want to count the number of occurences of a single character in a string, then you should look at using 'tr'.

    $str = 'ABABAABBA'; $count = $str =~ tr/A//; # $a = 5;

    If you omit the second arg to tr, it knows you are not changing the string, but still counts up the occurences for you, which makes this very efficient.

    After that you just have to see if the number is positive ($count > 0) and use Grandfather's test above to see if it is even ( !($count & 1) ).

    if ($count && !($count & 1)) { # Do something with this protein sequence }
Re: Check even numbers?
by jwkrahn (Abbot) on Aug 07, 2006 at 01:52 UTC
    Perhaps you could use a regular expression:
    print $sequence if $sequence =~ /^[^A]*(?:A[^A]*A)+[^A]*$/;
Re: Check even numbers?
by Corion (Patriarch) on Aug 07, 2006 at 06:53 UTC

      I assume you are aware, (and it probably should be mentioned), that the referenced method is (I assume), a joke along the same lines as the included method of finding tomorrow's date:

      sub tomorrow_date { sleep 86_400; return localtime(); }

      Without all the compile-time reductions, lazy evaluation etc. that most properly functional languages benefit from, this method of determining the eveness of a number is ludicrously inefficient--6000+ times slower than the normal method:

      #! perl -slw use strict; use Benchmark qw[ cmpthese ]; no warnings 'recursion'; sub odd { my $number = shift; return !even ($number); } sub even { my $number = abs shift; return 1 if $number == 0; return od +d ($number - 1); } cmpthese -5, { normal => q[ my $n; $_ & 1 and $n++ for 1 .. 1000; ], functional => q[ my $n; even( $_ ) and $n++ for 1 .. 1000; ], }; __END__ c:\test>junk s/iter functional normal functional 1.50 -- -100% normal 2.41e-004 625042% --

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Check even numbers?
by lima1 (Curate) on Aug 07, 2006 at 10:21 UTC
    EMBOSS pepstats is probably what you want.

    Bioperl supports starting of EMBOSS programs and parsing output files.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://565869]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-24 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found