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

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.
  • Comment on Re: Multiple replacements with the exception of previously made replacements.

Replies are listed 'Best First'.
Re^2: Multiple replacements with the exception of previously made replacements.
by rovf (Priest) on Aug 17, 2009 at 14:12 UTC
    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>
Re^2: Multiple replacements with the exception of previously made replacements.
by Bloodnok (Vicar) on Aug 17, 2009 at 14:24 UTC
    ...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 :-))