I have a string
$string= 'AbCdEf';
and I want to use the tr function to convert all the uppercase letters to lower case and all the lower case to upper case.... at the same time. I basically just want to reverse it to become.
aBcDeF
I came up with this line, but I'm not sure how to modify it to do what I want. Any help please?
$string=~ tr/A-Z/a-z/;
Thanks!
$string =~ tr/A-Za-z/a-zA-Z/;
At Tom's request, the Unicode-clean (or locales-clean) version:
You can do the full Unicode solution either this way:
or this way
Depending on what you want to do with something that changes case in both directions, like Dz, whose uppercase is DZ and whose lowercase is dz.
If you run the second of those two substitutions across this input:
it produces these results:
The only part that would be different (in that set) using the first function would be that the dz sequence would then look like this instead:
The reason you don’t want to use just an upper or lower test is because then you do unnecessary work, since there are plenty of cased code points that do not change case when casemapped. All of these, for example, are cased code points but which change neither when uppercased nor when lowercased:
So you would detect that they were upper- or lowercase, then call the inverse mapping function, then discover that nothing at all had changed. I figure, why bother?