How does the complement work with transliteration ?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I was wondering how tr/abcd/1234/cs is supposed to work... I intend it
to mean- if its anything other than abcd replace with ... I am not
sure what the right way is.

Without the /c,  tr/abcd/1234/s does the expected, replaces each of a
b c and d found, with their corresponding replacement characters. But
in the /c case I notices that any character that was not one of abcd
was replaed with 4, the last character in the REPLACEMENTSTRING 1234.

I tried to look at documentation - they have just one example of
replacing all that dont fit the search with a space. If any of you
have examples of how you use this thing with /c, i'd love to see them.

Re: How does the complement work with transliteration ?

Quoted text here. Click to load it

perldoc perlop:



        If the "/d" modifier is used, the REPLACEMENTLIST is always
        interpreted exactly as specified. Otherwise, if the
        REPLACEMENTLIST is shorter than the SEARCHLIST, the final
        character is replicated till it is long enough. If the
        REPLACEMENTLIST is empty, the SEARCHLIST is replicated. This
        latter is useful for counting characters in a class or for
        squashing character sequences in a class.

Using a replacement list of more than one character with /c will
give results that depend on what order the complement characters
are listed in the resulting complemented SEARCHLIST.

I don't think that's defined anywhere, but I would assume is would
be in ord() order though how that interacts with unicode strings
I have no idea and the assumption is vaguely supported by:

; perl -de1
main::(-e:1):   1
  DB<1> $s="abc"

  DB<2> x $s
0  "\c@\cA\cBabc"
  DB<3> $s=~tr/abc/123/c

  DB<4> x $s
0  '123abc'

I think "don't do that" applies here.

Sam Holden

Site Timeline