in reply to Re^4: Regex /g and interpolated lengths
in thread Regex /g and interpolated lengths

Running that code gives

Branchpoint: ctgac 34 ctgac 34 ctgac 34 ctgac 34 ctgac 34 ctgac 34

What were you expecting? If this is not the output you are getting, then I'd guess duff probably has the answer


Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

Replies are listed 'Best First'.
Re^6: Regex /g and interpolated lengths
by KaiAllard (Initiate) on Nov 15, 2005 at 22:37 UTC
    No sorry the machine is a Windonws system so no confusing \r or whatever. Yes what I want to get is all aligned Branchpoints. So it should be:
    ctgac 34 ctgat 18 ctaac 0 ctgac 34 ctgat 18 ctaac 0 ctgac 34 ctgat 18 ctaac 0 ctgac 34 ctgat 18 ctaac 0 ctgac 34 ctgat 18 ctaac 0 ctgac 34 ctgat 18 ctaac 0
    or something like that despite of the format. But it should find all Branchpoints ... not the first one and then surrender.
      My thinking was that it reaches the end of the string so it breaks up with only one found brachpoint. May it be the solution?

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

      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan