in reply to Multiple replacements with the exception of previously made replacements.

Use tr/ab/ba/ instead of s///?

This is presuming that your elements to match are single characters as in the example, however.

Replies are listed 'Best First'.
Re: Multiple replacements with the exception of previously made replacements.
by jthiel (Initiate) on Aug 17, 2009 at 14:02 UTC
    Wow, I'm impressed by how fast I got three awesome replies here, thanks to all of you so far.

    "This is presuming that your elements to match are single characters as in the example, however."

    Argh, I'm really sorry, I should've picked a better example here, I forgot about tr/// when choosing the 'a' and 'b' one.

    In my real life issue I want to replace
    every \bi\b with a 'you',
    every \byou\b with an 'i',
    every \bu\b with an 'i',
    every \byour\n with a 'my',
    every \bmy\b with a 'your',
    every \bam\b with an 'are',
    every \bare\b with an 'am',
    and every \br\b with an 'am'.

    "i think you really should've picked a better example for your perlmonks thread"
    =>
    "you think i really should've picked a better example for my perlmonks thread"



    I'm really sorry for the poor example in my inital post.
    Thanks guys.
      How about this:
      1. Create a hash which describes the transformations (i.e. $translate{'are'}='am' etc.)
      2. Use split to split your sentence into words.
      3. Use map to apply the translation of the words, using your %translate hash.
      4. Use join to put back the words into a sentence

      -- 
      Ronald Fischer <ynnor@mm.st>
      ...in which case, JavaFans excellent answer (I wish I'd have thought of it:-) is, IMO, the way to go.

      A user level that continues to overstate my experience :-))