Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Removing Flanking "N"s in a DNA String

by inman (Curate)
on Nov 07, 2005 at 13:43 UTC ( [id://506333]=note: print w/replies, xml ) Need Help??


in reply to Removing Flanking "N"s in a DNA String

A pair of substitutions with a regular expression anchored to the start and end of the string will help.
$dna = "NNNNATCGNNNTCGANNN"; $dna =~ s/^[N]+//; $dna =~ s/[N]+$//; print "$dna\n" __END__ ATCGNNNTCGA

Replies are listed 'Best First'.
Re^2: Removing Flanking "N"s in a DNA String
by ikegami (Patriarch) on Nov 07, 2005 at 13:52 UTC
    No need for the square brackets:
    $dna = "NNNNATCGNNNTCGANNN"; $dna =~ s/^N+//; $dna =~ s/N+$//; print "$dna\n" __END__ ATCGNNNTCGA
Re^2: Removing Flanking "N"s in a DNA String
by davidrw (Prior) on Nov 07, 2005 at 15:17 UTC
    As metioned already, no need for the character class.. Also, (although i tend to prefer the 2 separate statements as well), this can be written as one:
    $dna =~ s/(^N+|N+$)//g;
      Also,
      s/(?:^N+|N+$)//g;
      since we don't really need capturing. Which makes me think that ideally one may want it, instead, to see the text that has been substituted. But then he would only get the N's at ^ (if any) as $1 or . Which in turn make me wonder why there's this asymmetry between s/// and m//, since the latter, in list context and with /g does return all captures while the former just returns a true (or false) value in any context, precisely the numer of matches (or the empty string)...
Re^2: Removing Flanking "N"s in a DNA String
by blazar (Canon) on Nov 07, 2005 at 14:49 UTC
    Why do you need those charachter classes?
    s/^N+//, s/N+$// for $dna;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-19 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found