Several people have addressed the problem of forcing the first character of a string to be uppercase, but the problem as presented is more difficult. I don't have a solution, but I may be able to clarify the problem.

 

We do not wish to force the first character to be uppercase. We wish to swap characters $first and $second, but NOT swap their respective cases.

 

 

Character encoding differs by operating system.

 

ASCII

 65 to 90 = 'A' to 'Z', ASCII 97 to122 = 'a' to 'z'.

 Note 'A' - 'a' = -32 and you can get to any letter by counting up from 'A' or 'a'.

 

EBCDIC

 129 to 137 = 'a' to 'i',  145 to 153 = 'j' to 'r', 162 to 169 = 's' to 'z'

 177 to 201 = 'A' to 'I',  209 to 217 = 'J' to 'R', 226 to 233 = 'S' to 'Z'

 Note 'A' - 'a' = +48 AND gaps in letter coding make calculations hairy.

 

Unicode must complicate things further.

 

This is my understanding of the original problem Ing. Branislav Gerzo proposed.

 

Breaking the $temp variable constraint, a solution might be:

 

use strict;

use warnings;

 

my $first = 'B';

my $second = 'a';

 

# If $first is NOT uppercase, force $second lowercase, else force $second uppercase and hold in $temp

my $temp = (ord(uc($first))-ord($first)) ? lc($second) : uc($second);

 

# If $second is NOT uppercase, force $first lowercase, else force $first uppercase

$second = (ord(uc($second))-ord($second)) ? lc($first) : uc($first);

 

 

$first = $temp;

 

print $first, "\n";

print $second, "\n";

 

 

David Luke, Application Developer IV

Isocorp, Inc.

State Technology Office

100 Rhyne Building

2740 Centerview Drive

Tallahassee, Florida 32301

 

(850) 216-3746

 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to