in reply to Infinity loop

Well, that's because $_[0]->seq() continues to return strings that have "CG" in them. And it probably returns a new string each time, or a copy of a string (which would be new), and then the match tries again.

What you probably want is:

my $seq = $_[0]->seq(); while ($seq =~ /CG/) { ++$CG }
But I'm not sure about that. Are you trying to count the number of CG sequences? Then you want:
$CG += () = $_[0]->seq() =~ /CG/g;
I think that "+=()=" may be called the "pimply-goatse-operator". Not sure. Ignore that.

What is it that this code is really trying to accomplish?

Replies are listed 'Best First'.
Re^2: Infinity loop
by doob (Pilgrim) on Nov 20, 2005 at 16:36 UTC
    my $seq = $_[0]->seq(); while ($seq =~ /CG/) { ++$CG } is exactly what I had before my ...mentor...told me to use this other method...so I am still scratching my head in perplexion...he told me to seek the answer to how i can make the new line work without adding new lines of code. Thanks, though =d(o_o)b=
      Well you could avoid the temporary variable by using //g in a LIST context instead. I wouldn't recommend it if the temporary list was likely to be of non-trivial size.
      for ( $_[0]->seq() =~ /CG/g ) { ++$CG }
      If course if the body of your loop is really just ++$CG then all you are doing is counting the number of elements in the list. In that case the "pimply-goatse-operator" mentioned earlier in ths thread is what you want.
      If your string result is so huge that the copy really is a problem, you can temporarily make an alias like this:
      for my $seq ($_[0]->seq()) { # in case you need to reset the first match position # (avoid pos() on huge strings) # $seq =~ /\za/g; $CG++ while $seq =~ /CG/g; }
      update

      Interesting enough, due to the way the perl internals work, this doesn't save you memory compared to

      my $seq = $_[0]->seq();
      during the processing, but the gain is less wasted memory when $seq goes out of scope again (this isn't a memory leak, perl will reuse that memory if the scope is entered again)