[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] calculation in replacement string



On Thu, 14 Mar 1996, Peter Folk wrote:

> I need to do a search and replace, replacing on a function of one of the
> matched regexps...  Eg:
> 
> s/^([^:]*)/${$1+4}/;
> 
> to change "84:thingies" to "88:thingies".  Anybody got any ideas?
> The above actually replaces the 84 with the contents of the variable
> $88.  And, yes, I know I could do something like:
> s/^([^:]*)//;
> $_ = $`. $1+4 . $';
> 
> But doing that with a complicated regexp (especially with a /g modifier)
> is quite clunky, and nontrivial.

How about 

s/^([^:]*)/$1+4/e;

?  The 'e' modifier causes PERL to interpret the replacement string as an 
expression.

John Peterson -- University Networking Services -- Brigham Young University
Internet: John_Peterson@byu.edu                       Phone: (801) 378-5007