Regexp substitution with dynamic replacement string
am 13.11.2007 14:14:58 von 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
Re: Regexp substitution with dynamic replacement string
am 13.11.2007 14:20:09 von Peter Makholm
augukarl@yahoo.se writes:
> 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
Re: Regexp substitution with dynamic replacement string
am 13.11.2007 14:44:36 von augukarl
On 13 Nov, 14:20, Peter Makholm wrote:
> auguk...@yahoo.se writes:
> > 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
Ah, thanks! Works like a charm.
August