in reply to Re: for loops
in thread Count every 3rd base occurence

Why so complicated?
use strict; use warnings; my $string = "CTCCGGATCTAT"; my $counter = 0; $counter++ while $string =~ /..[GC]/gi; print "Found $counter occurences$/";
Oops, duh. I even tested. %-/
use strict; use warnings; my $string = "CTCCGGATCTAT"; my %flag = (C => 1, G => 1, A => 0, T => 0); my $counter = 0; $counter += $flag{$1} while $string =~ /..(.)/g; print "Found $counter occurences$/";

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^3: for loops
by Aristotle (Chancellor) on Dec 17, 2002 at 00:05 UTC
    Ugh, had to read BrowserUk's approach twice to see what was going on. Mine isn't as simple as it started out either - raaa, so obvious in hindsight: my $count = grep /[GC]/, $dna =~ /..(.)/g; There. It slices, it dices, it counts DNA bases. (Or is that RNA? Anyway.)

    Makeshifts last the longest.

      Much better than mine, and any of the others to my way of thinking, Clean, neat and obvious (to read).

      Should have been obvious to write too, but it wasn't.


      Examine what is said, not who speaks.

Re: Re^2: for loops
by BrowserUk (Patriarch) on Dec 16, 2002 at 22:08 UTC

    Why so complicated? ;)

    my $count=0; $count += substr($dna, $_, 1) =~ /[CG]/ for map 3 * $_, 1 .. length($d +na)/3; print $count;

    Examine what is said, not who speaks.

Re^3: for loops
by Mr. Muskrat (Canon) on Dec 16, 2002 at 21:58 UTC

    Why so complicated?

    1. TIMTOWTDI
    2. The comments shed light on how it works
    3. It worked the first time. q-:
    4. Why not?