in reply to stupid/simple mistake

To count the number of occurrences of GATC in your string (this is a FAQ):
perldoc -q count
use warnings; use strict; my $substring = 'GATC'; my $count = 0; my $sequence = 'GAGAGACCCCGATCGAGAGACCCGATCFGAGAVCTGATCCCC'; while ($sequence =~ /$substring/g) { $count++ } print "count = $count\n"; __END__ count = 3
See also: How do I compose an effective node title?

Replies are listed 'Best First'.
Re^2: stupid/simple mistake
by JavaFan (Canon) on Oct 18, 2011 at 13:05 UTC
    while ($sequence =~ /$substring/g) { $count++ }
    You never learned how to add numbers without using your fingers? ;-)

    I don't understand the urge to count one-by-one -- and you're not the first poster in this thread to do so.

    while (<FILE>) { $count += () = /$substring/g; }
    ought to do it.
      #!/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 !

        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 ;)
        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.