Re: s/A/B/ and s/B/C/ but don't want A -> C (was: FAQ 6.4 I put a regular expression into $/ but it didn't work. What's wrong?)



_
Ted Zlatanov (tzz@xxxxxxxxxxxx) wrote on VCCXXXII September MCMXCIII in
<URL:news:86k5myzm6t.fsf_-_@xxxxxxxxxxxx>:
&& On Thu, 27 Dec 2007 21:35:11 +0000 Ben Morrow <ben@xxxxxxxxxxxx> wrote:
&&
&& BM> Quoth PerlFAQ Server <brian@xxxxxxxxxxxxxx>:
&& >>
&& >> 6.4: I put a regular expression into $/ but it didn't work. What's wrong?
&& >>
&& >> Up to Perl 5.8.0, $/ has to be a string. This may change in 5.10, but
&& >> don't get your hopes up. Until then, you can use these examples if you
&&
&& BM> s/8/10/; s/10/12/;
&&
&& You want that backwards :)
&&
&& Actually, I've often run into the need for parallel edits like this,
&& where you want to s/A/B/ and s/B/C but you don't want A to become C.
&& With complex operations or operations you don't know in advance, proper
&& ordering becomes impossible. I usually handle it with function calls:
&& s/[ABC]/replacement(\1)/e or something like that. Is there a better
&& approach anyone can recommend?


If A, B and C are just single characters, tr/AB/BC/ will do.

Otherwise, I'd do:

my %replacement = (A => B, B => C);
s/(A|B)/$replacement{$1}/g;


Abigail
--
perl -wle 'eval {die [[qq [Just another Perl Hacker]]]};; print
${${${@}}[$#{@{${@}}}]}[$#{${@{${@}}}[$#{@{${@}}}]}]'
.