elwarren has asked for the wisdom of the Perl Monks concerning the following question:

Simple question. Can I execute on the right side of a regex? I'm searching for a word, and I want to double whatever number comes after it. This doesn't do what I want:
$var =~ s/WORD\s(\d+)/WORD (2 * $1)/;
I know I can do it with an if statement and $1, but now that I've started down this path...

Replies are listed 'Best First'.
(Ovid) Re: regex and multiplication
by Ovid (Cardinal) on Sep 21, 2000 at 20:19 UTC
    Yes, add the /e switch:
    $var =~ s/WORD\s(\d+)/"WORD " . (2 * $1)/e;
    See perlre for details.

    Cheers,
    Ovid

    Update: Fixed post in response to tye's clarification.

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

      You should quote the parts of the replacement you don't want executed:

      s/WORD\s(\d+)/"WORD ".(2*$1)/e;

      Or, how I'd write it:

      s/(WORD\s)(\d+)/$1.(2*$2)/e;
              - tye (but my friends call me "Tye")
Re: regex and multiplication
by elwarren (Priest) on Sep 21, 2000 at 20:26 UTC
    *doh!* I knew it was something easy like that. I don't see it anywhere on the perlre page, because I must have gone over it three times this morning. As soon as I read your reply I found it in the perlop page.
    Thanks, good response time!