in reply to Re^6: Regex /g and interpolated lengths
in thread Regex /g and interpolated lengths
Your code measures the length of the line only once and then matches the given pattern and any characters up to the end of the line, after which it starts at the beginning of the next line. This should do what you want:
while(my $line=<FILE>) { my $linecopy=$line; while ($line=~m/(ct[ag]a[ct])/g) { my $match=$1; my ($rest)=$linecopy=~m/(?:$match)(.*)$/; print "$match\t".length($rest)."\t"; } print "\n"; }
Update: Or, if you're into recursion:
sub matchit { my $data=shift; if ($data =~ m/(ct[ag]a[ct])(.*)$/) { print "$1\t".length($2)."\t"; matchit($2); } else { print "\n"; } } matchit($_) while(<FILE>);
|
|---|