in reply to Re^2: stupid/simple mistake
in thread stupid/simple mistake

#!/usr/bin/perl use warnings; use strict; open (FILE,"sequence.txt"); my $substring = 'GATC'; my$i=0; my$count; my $sequence; my$result; my $number=0; my $string; my $offset = 0; my @results; while ($sequence=<FILE>){ foreach($sequence =~ /$substring/g) { #print "malakas\n"; $count += () = /$substring/g; $number++; my $result = index($sequence, $substring, $offset); while ($result != -1) { push (@results, $result); print "Found $substring at $result\n"; $offset = $result + 1; $result = index($sequence, $substring, $offset); } #print "$count\n"; } #print "$result\n"; } foreach $result(@results){ my $sum } print "There are $count GATCs";

ok now I have the positions into an array, but I need to calculate the distances between them, which is the sum of result(1)-result(0) + result(2)-result(1) +.... etc etc. I don't really know how to write that in code. any ideas? Thanks in advance again !

Replies are listed 'Best First'.
Re^4: stupid/simple mistake
by Caio (Acolyte) on Oct 18, 2011 at 16:05 UTC
    How about:
    my @distances =(); for(my $c=1; $c< scalar @results; $c++){ my $distances[($c-1)] = $results[$c] - $results[($c-1)]; }


    Just typed the code in the coment, so you have to try it an tweak it as necessary. But it SHOULD work just fine ;)
Re^4: stupid/simple mistake
by JavaFan (Canon) on Oct 18, 2011 at 22:04 UTC
    while ($sequence=<FILE>){ foreach($sequence =~ /$substring/g) { #print "malakas\n"; $count += () = /$substring/g;
    Hmmm, copy-and-paste is too hard? I did not write a double loop. And your result just doesn't make much sense. In fact, it doesn't make any sense.

    Considering that spoon-feeding isn't working for you, I don't know what else to do.