Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi Guys,

A Simple Substitution Problem

    my $dna =

    # I want to replace asterisk(*) with dot(.) if 10 or more
asterisks occur together

    $dna =~ s/\*/./g;

    print "$dna\n";

    # output


    As you can see 10 or more asterisk are replaced with dot but what
I want is this


How to do it


Re: Substitution

$dna=3D~s/\*/"." x length($&)/eg;

Quoted text here. Click to load it

Re: Substitution

  x> $dna=~s/\*/"." x length($&)/eg;

don't use $& as it will slow down all the other regexes in your
program. this is a known problem and trivial to get around. just
explicitly grab the matched string and refer to it with $1

    $dna =~ s/(\*)/ '.' x length($1) /eg;


Uri Guttman  ------  -------- --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- ---------
---------  Gourmet Hot Cocoa Mix  ---- ---------

Re: Substitution

Vishal G wrote:

Quoted text here. Click to load it
Quoted text here. Click to load it

Here's one way, off the top of my head:

$dna =~ s/(\*)/'.' x length($1)/eg;

It replaces 10 (or more) instances of \* with ., based on the actual
length of the (\*) match, replacing each one, with no fixed length
limit (so long as there are 10 or more instances).  Just sticking with
your original regex substitution example solution.
Tim Greer, CEO/Founder/CTO,, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Re: Substitution

Quoted text here. Click to load it

So you wan't 5,000,000 asterisks replaced with 5,000,000 dots?


Re: Substitution

On Wed, 5 Nov 2008 22:17:37 -0800 (PST), Vishal G wrote:
Quoted text here. Click to load it

You seem to be working with genomes.  Have you looked at bioperl?

I don't know whether it will solve your problem, but I suspect it may.

Rocco Caputo - /

Site Timeline