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

I am a perl beginner and need some help with regex Input for this code is : aatgatgataaggtaaggtatgatgatgatgatgatagtagannnnnnnnnatgcatga'/atgca.atgactagca/atgactagcaaggtaaggtaaggtaaggtaaggtatgatgatgannnn./atgatgactagactgacaaggtaaggtaaggtatgatgatgatcgatgacgat... and so on

Here i am trying to assign input file as a scalar variable and trying to find a match of "aaggtaaggt" and then skip some 100 characters whether they are alphabets or symbols or any wildcard characters after skipping exact 100 characters, i am again asking code to start search for match again and every time it finds a match , i am counting and asking to print..

As of my knowledge i have tried using substr of match as pos1 and had set offset for 100 and then assigned that as initial pos for reading second match , but failed to get the correct output, then tried here post match as $' but doubt whether it is correct or not.

#!/usr/bin/perl $count1 = 0; open (FILE, "INPUT") || die "cannot open $!\n"; while ($line = <FILE>){ if(($line=~m/(aaggt){2}/ig)&&($'=~m/([atgcn]{100,})/i)) { $count1 ++ ; print " " ,$1, "\t"; print "count1 \t " ,$count1 , "\n" ; } }
Please, help me figuring out this task. Thanks

Replies are listed 'Best First'.
Re: Need Help with reg ex
by GrandFather (Saint) on Oct 28, 2009 at 04:07 UTC

    If I understand what you are trying to achieve then you can do it using a single regular expression match:

    use strict; use warnings FATAL => 'all'; my $count1 = 0; my $line = do {local $/; <DATA>;}; $line =~ s/\n//g; while (($line =~ m/(?:aaggt){2}([atgcn]{100,})/ig)) { $count1++; print " $1\tcount1 \t $count1\n"; } __DATA__ aatgatgataaggtaaggtatgatgatgatgatgatagtagannnnnnnnnatgcatgaatgcaatgact +agcaatgac tagcaaggtaaggtaaggtaaggtaaggtatgatgatgannnnatgatgactagactgacaaggtaaggt +aaggtatga tgatgatcgatgacgat

    As an aside, note that you should always use strictures (use strict; use warnings;). You should also use the three parameter version of open and use lexical file handles:

    open my $inFile, '<', $fileName or die "cannot open $fileName: $!\n";

    True laziness is hard work
Re: Need Help with reg ex
by bichonfrise74 (Vicar) on Oct 28, 2009 at 04:07 UTC
    Based on how I understand your question, this is how I would do it.
    #!/usr/bin/perl use strict; my $count = 0; while (<DATA>) { while ( /(aaggt)/g ) { $count++; print "$1 - Count: $count\n"; print "Found at position : " . pos(), "\n"; pos() += 100; } } __DATA__ aatgatgataaggtaaggtatgatgatgatgatgatagtagannnnnnnnnatgcatga'/atgca.atg +actagca/atgactagcaaggtaaggtaaggtaaggtaaggtatgatgatgannnn./atgatgactag +actgacaaggtaaggtaaggtatgatgatgatcgatgacgat