in reply to Re: multi find/replace
in thread multi find/replace

Going through it all just once has more benefits than just speed. Imagine for example, that you have to replace every occurrence of "A" with "B", of "B" with "C", and of "C" with "A" — thus swapping places. That's extremely easy if you do it in one go, and quite impossible to do it in three iterative substitutions.

Good:

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

Bad:

s/A/B/; s/B/C/; s/C/A/;
For the latter, everything that was "A", "B" or "C", will now be "A".

Replies are listed 'Best First'.
Re: Re: Re: multi find/replace
by Limbic~Region (Chancellor) on Feb 10, 2004 at 22:28 UTC
    bart,
    While you have a valid point, it is not applicable to this problem. The code you state as bad is what the AM already stated as working. While I do not promote my solution as the best one to use, it certainly does save keystrokes.

    Cheers - L~R