How does the complement work with transliteration ?

A

anita

Hello
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.
Thanks
anita
 
S

Sam Holden

Hello
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.

perldoc perlop:

tr/SEARCHLIST/REPLACEMENTLIST/cds
y/SEARCHLIST/REPLACEMENTLIST/cds

...

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="\0\1\2abc"

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top