Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Infinity loop

by doob (Pilgrim)
on Nov 20, 2005 at 16:36 UTC ( [id://510253]=note: print w/replies, xml ) Need Help??


in reply to Re: Infinity loop
in thread Infinity loop

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=

Replies are listed 'Best First'.
Re^3: Infinity loop
by nobull (Friar) on Nov 20, 2005 at 17:59 UTC
    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.
Re^3: Infinity loop
by thospel (Hermit) on Nov 20, 2005 at 22:03 UTC
    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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://510253]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-24 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found