- 论坛徽章:
- 0
|
回复 #8 hdc1112 的帖子
其实也可以多个同时替换。比如:
$x =~ tr/0-9/QERTYUIOPX/; # Digits to letters.
$x =~ tr/A-Z/a-z/; # Convert to lowercase.
并且,不一定需要使用 Slash / 作为 delimiter (分隔符)
比如:
$x =~ tr!xianer!XIANER!;
$x =~ tr ianer:XIANER:;
另外, tr还有一些选项。比如 s: 压缩多个重复字符为一个
举例:
------------
#!/usr/bin/perl -w
use strict;
my $text = 'good cheese';
$text =~ tr/oe/ue/s; #注意这里的 s
print "$text\n";
# Output is: gud chese
另一个是 d 选项。用来删除字符。
比如:
--------------
my $big = 'vowels are useful';
$big =~ tr/aeiou/AEI/d;
print "$big\n";
# The first three vowels are made uppercase. The other two, which have no replacement character, are deleted because of the "d".
#输出结果是: vwEls ArE sEfl
[ 本帖最后由 xianer2 于 2008-7-30 11:05 编辑 ] |
|