Regexp substitution with dynamic replacement string

A

augukarl

Hi everyone,

Let's say, for instance, that I would like to replace each instance of
the letter 'e' in this sentence with a random integer between 0 and 9.
How would I do that? The following does not work obviously:

my $s = 'Let's say, for instance...';
while ($s =~ /e/g) {
$0 = int(rand(10));
}


Regards,

August
 
P

Peter Makholm

Let's say, for instance, that I would like to replace each instance of
the letter 'e' in this sentence with a random integer between 0 and 9.
How would I do that? The following does not work obviously:

No, but by using the /e-modifier to a substitution you can use the
result of an expression as replacement. Read the parts of 'perldoc
perlop' about s/PATTERN/REPLACEMENT/egimosx

//Makholm
 
A

augukarl

No, but by using the /e-modifier to a substitution you can use the
result of an expression as replacement. Read the parts of 'perldoc
perlop' about s/PATTERN/REPLACEMENT/egimosx

Ah, thanks! Works like a charm.


August
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top