Substitution

V

Vishal G

Hi Guys,

A Simple Substitution Problem

my $dna =
"***********acgtgcta*****atctgat******actgtaaa***tttt**cccc******ccccc******";

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

$dna =~ s/\*{10,}/./g;

print "$dna\n";

# output

.acgtgcta*****atctgat******actgtaaa***tttt**cccc******ccccc******

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

...........acgtgcta*****atctgat******actgtaaa***tttt**cccc******ccccc******


How to do it

Vishal
 
T

Tim Greer

Vishal said:
Hi Guys,

A Simple Substitution Problem

my $dna =
"***********acgtgcta*****atctgat******actgtaaa***tttt**cccc******ccccc******";

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

$dna =~ s/\*{10,}/./g;

print "$dna\n";

# output

.acgtgcta*****atctgat******actgtaaa***tttt**cccc******ccccc******

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

...........acgtgcta*****atctgat******actgtaaa***tttt**cccc******ccccc******


How to do it

Vishal

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

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

It replaces 10 (or more) instances of \* with ., based on the actual
length of the (\*{10,}) 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.
 
U

Uri Guttman

x> $dna=~s/\*{10,}/"." 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/(\*{10,})/ '.' x length($1) /eg;

uri
 
S

sln

Hi Guys,

A Simple Substitution Problem

my $dna =
"***********acgtgcta*****atctgat******actgtaaa***tttt**cccc******ccccc******";

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

$dna =~ s/\*{10,}/./g;

print "$dna\n";

# output

.acgtgcta*****atctgat******actgtaaa***tttt**cccc******ccccc******

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

...........acgtgcta*****atctgat******actgtaaa***tttt**cccc******ccccc******


How to do it

Vishal

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

sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top