• 博客访问： 458737
• 博文数量： 142
• 博客积分： 4126
• 博客等级： 上校
• 技术积分： 1545
• 用 户 组： 普通用户
• 注册时间： 2008-02-22 10:03

2011年（8）

2010年（7）

2009年（64）

2008年（63）

2009-04-24 10:37:41

1.1.1 提出问题

1.1.2 解决方案

 `\$big = uc(\$little); # "bo peep" -> "BO PEEP"\$little = lc(\$big); # "JOHN" -> "john"\$big = "\U\$little"; # "bo peep" -> "BO PEEP"\$little = "\L\$big"; # "JOHN"    -> "john"`

 `\$big = "\u\$little"; # "bo" -> "Bo"\$little = "\l\$big"; # "BoPeep" -> "boPeep"`

1.1.3 讨论

 `\$beast = "dromedary";# 大写 \$beast中的一些部分\$capit = ucfirst(\$beast); # Dromedary\$capit = "\u\L\$beast"; # (same)\$capall = uc(\$beast); # DROMEDARY\$capall = "\U\$beast"; # (same)\$caprest = lcfirst(uc(\$beast)); # dROMEDARY\$caprest = "\l\U\$beast"; # (same)`

 `# 使每个单词首字母大写，其余字母小写\$text = "thIS is a loNG liNE";\$text =~ s/(\w+)/\u\L\$1/g;print \$text;# \$>This Is A Long Line`

 `if (uc(\$a) eq uc(\$b)) { # or "\U\$a" eq "\U\$b"    print "a and b are the same\n";}`

 `#!/usr/bin/perl -p  # randcap: filter to randomly capitalize 20% of the letters  # call to srand( ) is unnecessary as of v5.4  BEGIN { srand(time( ) ^ (\$\$ + (\$\$<<15))) }  sub randcase { rand(100) < 20 ? "\u\$_[0]" : "\l\$_[0]" }  s/(\w)/randcase(\$1)/ge;`

% randcap < genesis | head -9
boOk 01 genesis
001:001 in the BEginning goD created the heaven and tHe earTh.

001:002 and the earth wAS without ForM, aND void; AnD darkneSS was
upon The Face of the dEEp. and the spIrit of GOd movEd upOn
tHe face of the Waters.
001:003 and god Said, let there be ligHt: and therE wAs LigHt.

Code point  Written   Meaning
01F1        DZ        LATIN CAPITAL LETTER DZ
01F2        Dz        LATIN CAPITAL LETTER D WITH SMALL LETTER Z
01F3        dz        LATIN SMALL LETTER DZ

1.2.4 参考
uc, lc, ucfirst, 和 lcfirst functions 在 perlfunc(1) 跟大骆驼书 第29章可以看到;
\L, \U, \l, 和 \u 转义在perlop(1)里面的 "Quote and Quote-like Operators" 一节跟 大骆驼书第5章有。

1.2.4 测试程序

 `#-----------------------------use locale; # needed in 5.004 or above\$big = uc(\$little); # "bo peep" -> "BO PEEP"\$little = lc(\$big); # "JOHN" -> "john"\$big = "\U\$little"; # "bo peep" -> "BO PEEP"\$little = "\L\$big"; # "JOHN" -> "john"#-----------------------------\$big = "\u\$little"; # "bo" -> "Bo"\$little = "\l\$big"; # "BoPeep" -> "boPeep" #-----------------------------use locale; # needed in 5.004 or above\$beast = "dromedary";# capitalize various parts of \$beast\$capit = ucfirst(\$beast); # Dromedary\$capit = "\u\L\$beast"; # (same)\$capall = uc(\$beast); # DROMEDARY\$capall = "\U\$beast"; # (same)\$caprest = lcfirst(uc(\$beast)); # dROMEDARY\$caprest = "\l\U\$beast"; # (same)#-----------------------------# capitalize each word's first character, downcase the rest\$text = "thIS is a loNG liNE";\$text =~ s/(\w+)/\u\L\$1/g;print \$text;This Is A Long Line#-----------------------------if (uc(\$a) eq uc(\$b)) {    print "a and b are the same\n";}#-----------------------------# download the following standalone program#!/usr/bin/perl -p# randcap: filter to randomly capitalize 20% of the letters# call to srand() is unnecessary in 5.004BEGIN { srand(time() ^ (\$\$ + (\$\$ << 15))) }sub randcase { rand(100) < 20 ? "\u\$_[0]" : "\l\$_[0]" }s/(\w)/randcase(\$1)/ge;#% randcap < genesis | head -9#boOk 01 genesis###001:001 in the BEginning goD created the heaven and tHe earTh.## ##001:002 and the earth wAS without ForM, aND void; AnD darkneSS was##     upon The Face of the dEEp. and the spIrit of GOd movEd upOn##     tHe face of the Waters.###001:003 and god Said, let there be ligHt: and therE wAs LigHt.#-----------------------------sub randcase {    rand(100) < 20 ? ("\040" ^ \$_[0]) : \$_[0];}#-----------------------------\$string &= "\177" x length(\$string);#-----------------------------`