Perl using tr function to convert to uppercase and lowercase and vice versa at the same time?

I have a line

$string= 'AbCdEf'; 

and I want to use the tr function to convert all uppercase letters to lowercase and all lowercase to uppercase .... at the same time. I just want to change it to become.

 aBcDeF 

I came up with this line, but I'm not sure how to change it to do what I want. Any help please?

 $string=~ tr/AZ/az/; 

Thanks!

+7
source share
3 answers

$string =~ tr/A-Za-z/a-zA-Z/;

+10
source

In Tom's request, the version of Unicode-clean (or local-clean):

 s/([[:upper:]])|([[:lower:]])/defined $1 ? lc $1 : uc $2/eg 
+11
source

You can make a complete Unicode solution like this:

  s / (\ p {CWU}) |  (\ p {CWL}) / defined $ 1?  uc $ 1: lc $ 2 / gex;

or in this way

  s / (\ p {CWL}) |  (\ p {CWU}) / defined $ 1?  lc $ 1: uc $ 2 / gex;

Depending on what you want to do with something that changes the case in both directions, for example, Dz, whose upper case is DZ and the lower case is dz.

If you run the second of these two permutations on this input:

  @ 0040 COMMERCIAL AT
      © 00A9 COPYRIGHT SIGN
      Å 212B ANGSTROM SIGN
      ⒜ 249C PARENTHESIZED LATIN SMALL LETTER A
      Ⓐ 24B6 CIRCLED LATIN CAPITAL LETTER A
      Ⓐ 24D0 CIRCLED LATIN SMALL LETTER A
      A FF21 FULLWIDTH LATIN CAPITAL LETTER A
      a FF41 FULLWIDTH LATIN SMALL LETTER A
      Ⓒ 24B8 CIRCLED LATIN CAPITAL LETTER C
      Ⓒ 24D2 CIRCLED LATIN SMALL LETTER C
      Dz 01F1 LATIN CAPITAL LETTER DZ
      Dz 01F2 LATIN CAPITAL LETTER D WITH SMALL LETTER Z
      dz 01F3 LATIN SMALL LETTER DZ
      Ⅲ 2172 SMALL ROMAN NUMERAL THREE
      S 0053 LATIN CAPITAL LETTER S
      s 0073 LATIN SMALL LETTER S
      ſ 017F LATIN SMALL LETTER LONG S
      ⒮ 24AE PARENTHESIZED LATIN SMALL LETTER S
      Ⓢ 24C8 CIRCLED LATIN CAPITAL LETTER S
      Ⓢ 24E2 CIRCLED LATIN SMALL LETTER S
      Ꞅ A784 LATIN CAPITAL LETTER INSULAR S
      ꞅ A785 LATIN SMALL LETTER INSULAR S
      ß 00DF LATIN SMALL LETTER SHARP S
      ẞ 1E9E LATIN CAPITAL LETTER SHARP S
      Ⅶ 2166 ROMAN NUMERAL SEVEN
      Ⅻ 217B SMALL ROMAN NUMERAL TWELVE

he gives the following results:

  @ 0040 commercial at
      © 00a9 copyright sign
      å 212b angstrom sign
      ⒜ 249c parenthesized latin small letter a
      Ⓐ 24b6 circled latin capital letter a
      Ⓐ 24d0 circled latin small letter a
      a ff21 fullwidth latin capital letter a
      A ff41 fullwidth latin small letter a
      Ⓒ 24b8 circled latin capital letter c
      Ⓒ 24d2 circled latin small letter c
      dz 01f1 latin capital letter dz
      dz 01f2 latin capital letter d with small letter z
      Dz 01f3 latin small letter dz
      Ⅲ 2172 small roman numeral three
      s 0053 latin capital letter s
      S 0073 latin small letter s
      S 017f latin small letter long s
      ⒮ 24ae parenthesized latin small letter s
      Ⓢ 24c8 circled latin capital letter s
      Ⓢ 24e2 circled latin small letter s
      ꞅ a784 latin capital letter insular s
      Ꞅ a785 latin small letter insular s
      SS 00df latin small letter sharp s
      ß 1e9e latin capital letter sharp s
      Ⅶ 2166 roman numeral seven
      Ⅻ 217b small roman numeral twelve

The only part that will be different (in this set) using the first function will be that instead the dz sequence will look like this:

  dz 01f1 latin capital letter dz
      Dz 01f2 latin capital letter d with small letter z
      Dz 01f3 latin small letter dz

The reason you don’t want to use only the top or bottom test is because then you are doing unnecessary work, as there are many tiled code points that do not change the case when casemapped. All of them, for example, are code endpoints, but which do not change in either upper or lower case:

  ª 00AA FEMININE ORDINAL INDICATOR
      ᴬ 1D2C MODIFIER LETTER CAPITAL A
      ᴀ 1D00 LATIN LETTER SMALL CAPITAL A
      ℂ 2102 DOUBLE-STRUCK CAPITAL C
      ᴰ 1D30 MODIFIER LETTER CAPITAL D 
      ʣ 02A3 LATIN SMALL LETTER DZ DIGRAPH
      ʤ 02A4 LATIN SMALL LETTER DEZH DIGRAPH
      ℇ 2107 EULER CONSTANT
      ɘ 0258 LATIN SMALL LETTER REVERSED E
      ɞ 025E LATIN SMALL LETTER CLOSED REVERSED OPEN E
      ℊ 210A SCRIPT SMALL G
      ɡ 0261 LATIN SMALL LETTER SCRIPT G
      ɢ 0262 LATIN LETTER SMALL CAPITAL G
      ʰ 02B0 MODIFIER LETTER SMALL H
      ℋ 210B SCRIPT CAPITAL H
      ℎ 210E PLANCK CONSTANT 
      ℹ 2139 INFORMATION SOURCE
      ʲ 02B2 MODIFIER LETTER SMALL J
      ℳ 2133 SCRIPT CAPITAL M
      º 00BA MASCULINE ORDINAL INDICATOR
      ɸ 0278 LATIN SMALL LETTER PHI
      ĸ 0138 LATIN SMALL LETTER KRA
      ʏ 028F LATIN LETTER SMALL CAPITAL Y
      ℼ 213C DOUBLE-STRUCK SMALL PI

So you find that they were upper or lower case, then call the inverse mapping function, and then find that nothing has changed. I guess why bother?

+7
source

All Articles