stringZ has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monks!
I have the following problem to solve in perl.
Got an array in which there are strings (words), for example
@array = ("deemon", "jeez", "steer", "caaaaar", "bbbbb", "bbbbbbbbbbb");

I have to replace every character that occurs more than one and their occurences are even with XY and write out the result.

deemon => dXYmon
jeez => JXYz
steer => stXYr
caaaaar => cXYXYar
bbbbb => XYXYb
bbbbbbbbbb => XYXYXYXYXYb


Tried this code:

foreach(@array) {
    $_ =~ s/(\w+)\1+/01$1$2/g;
    print "$_ ";
}

it works for a few, but it doesn't for bbbbbbbbbbbb.
What could be a real solution (using substitution/translation if possible)?

Thanks
stringZ
  • Comment on Replace every character that occurs more than one and their occurences are even

Replies are listed 'Best First'.
Re: Replace every character that occurs more than one and their occurences are even
by almut (Canon) on Apr 28, 2009 at 21:16 UTC

    This would work, for example:

    foreach(@array) { $_ =~ s/(\w)\1/XY/g; print "$_ "; # --> dXYmon jXYz stXYr cXYXYar XYXYb XYXYXYXYXY +b }
      Thanks, this one works :)
Re: Replace every character that occurs more than one and their occurences are even
by graff (Chancellor) on Apr 28, 2009 at 22:22 UTC
    Based on the description given in the OP, almut has given the "right answer", but Corion has asked the right question, because the OP code seems to bear no relation to the description of the task, and it has some problems.

    The s/// operation in your code contains only one set of parens in the regex match, but uses $2 in the replacement, which will always be an empty string. There's no mention of "XY" in the replacement. And given this type of regex pattern:

    /(\w+)\1+/
    strings that will match it include: "aa", "abab", "ababab", "abcabc", "abcabcabc", "abcdabcd", etc. -- that is, whenever any string of one or more letters is repeated one or more times -- but there will be quirks: "abababab" will be matched as "abab" occurring twice (not "ab" occurring four times).
Re: Replace every character that occurs more than one and their occurences are even
by Corion (Patriarch) on Apr 28, 2009 at 21:06 UTC

    Can you please explain in the code you used what the substitution is doing and how it relates to the goal you're trying to achieve?

    Also, please show how the code you posted "works for a few" (examples?) and how it doesn't for bbbbbbbbbbbb.