How can you embed a function inside a replacement operator ?

N

neilsolent

Hi
Sorry if this is a basic question.
How can you embed a function inside a replacement operator ?

e.g.:
####################################
sub conv()
{
return $_[0] * 2;
}


$test = 'abc125abc';

$test =~ s/(\d+)/&conv($1)/g;

print $test . '\n';
####################################

The output of this script is:

abc&conv(123)abc

Whereas I would like it to be:

abc250abc


Many thanks,
Neil
 
P

Peter Makholm

neilsolent said:
Sorry if this is a basic question.
How can you embed a function inside a replacement operator ?

Look at the /e modifier. It is documented in 'perldoc perlop'.

//Makholm
 
N

nolo contendere

Hi
Sorry if this is a basic question.
How can you embed a function inside a replacement operator ?

e.g.:
####################################
sub conv()
{
return $_[0] * 2;

}

$test = 'abc125abc';

$test =~ s/(\d+)/&conv($1)/g;

print $test . '\n';
####################################

The output of this script is:

abc&conv(123)abc

Whereas I would like it to be:

abc250abc

my $test = 'abc125def';
$test =~ s/(\d+)/conv($1)/ge;

print $test, "\n";

sub conv {
my ( $val ) = @_;
return $val * 2;
}


__OUTPUT__

bash-2.03$ ./replace_func.pl
abc250def
bash-2.03$
 
N

neilsolent

Look at the /e modifier. It is documented in 'perldoc perlop'.

//Makholm

Thanks for that guys, thought it would be simple. I did do a search
first (honest!)
 
N

nolo contendere

Thanks for that guys, thought it would be simple. I did do a search
first (honest!)

np. sometimes it's hard to phrase your question for a search engine,
especially if you lack the correct key word. also, your post was
exemplary in that you posed a cogent question, showed your attempt
that was as short as possible and as long as it needed to be, showed
your results, and showed what you expected.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top