How can you embed a function inside a replacement operator ?
How can you embed a function inside a replacement operator ?
am 27.11.2007 16:08:01 von 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
Re: How can you embed a function inside a replacement operator ?
am 27.11.2007 16:12:07 von Peter Makholm
neilsolent writes:
> 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
Re: How can you embed a function inside a replacement operator ?
am 27.11.2007 16:13:47 von it_says_BALLS_on_your forehead
On Nov 27, 10:08 am, neilsolent wrote:
> 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$
Re: How can you embed a function inside a replacement operator ?
am 27.11.2007 16:40:18 von neilsolent
On 27 Nov, 15:12, Peter Makholm wrote:
> neilsolent writes:
> > 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
Thanks for that guys, thought it would be simple. I did do a search
first (honest!)
Re: How can you embed a function inside a replacement operator ?
am 27.11.2007 16:44:39 von it_says_BALLS_on_your forehead
On Nov 27, 10:40 am, neilsolent wrote:
> On 27 Nov, 15:12, Peter Makholm wrote:
>
> > neilsolent writes:
> > > 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
>
> 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.