6
A
回答
13
unpack
将比split
和ord
更高效,因为它不必让一帮暂时1字符串:
use utf8;
my $str = '中國c'; # Chinese language of china
my @codepoints = unpack 'U*', $str;
print join(',', @codepoints) . "\n"; # prints 20013,22283,99
快速基准测试显示它比split+ord
快3倍左右:
use utf8;
use Benchmark 'cmpthese';
my $str = '中國中國中國中國中國中國中國中國中國中國中國中國中國中國c';
cmpthese(0, {
'unpack' => sub { my @codepoints = unpack 'U*', $str; },
'split-map' => sub { my @codepoints = map { ord } split //, $str },
'split-for' => sub { my @cp; for my $c (split(//, $str)) { push @cp, ord($c) } },
'split-for2' => sub { my $cp; for my $c (split(//, $str)) { $cp = ord($c) } },
});
结果:
Rate split-map split-for split-for2 unpack
split-map 85423/s -- -7% -32% -67%
split-for 91950/s 8% -- -27% -64%
split-for2 125550/s 47% 37% -- -51%
unpack 256941/s 201% 179% 105% --
的差异不太明显与较短的字符串,但unpack
仍然是两倍以上的速度。 (split-for2
比其他分裂速度更快一点,因为它不建立码点的列表。)
3
foreach my $c (split(//, $str))
{
print ord($c), "\n";
}
或压制成单行:my @chars = map { ord } split //, $str;
Data::Dumper版,这将产生:
$VAR1 = [
20013,
22283,
99
];
3
要让UTF8在源代码中承认的,你必须use utf8;
事先:
$ perl
use utf8;
my $str = '中國c'; # Chinese language of china
foreach my $c (split(//, $str))
{
print ord($c), "\n";
}
__END__
20013
22283
99
以上简洁,
print join ',', map ord, split //, $str;
2
http://www.perl.com/pub/2012/04/perlunicook-standard-preamble.html
#!/usr/bin/env perl
use utf8; # so literals and identifiers can be in UTF-8
use v5.12; # or later to get "unicode_strings" feature
use strict; # quote strings, declare variables
use warnings; # on by default
use warnings qw(FATAL utf8); # fatalize encoding glitches
use open qw(:std :utf8); # undeclared streams in UTF-8
# use charnames qw(:full :short); # unneeded in v5.16
# http://perldoc.perl.org/functions/sprintf.html
# vector flag
# This flag tells Perl to interpret the supplied string as a vector of integers, one for each character in the string.
my $str = '中國c';
printf "%*vd\n", ",", $str;
相关问题
- 1. 将字符串转换为UTF8与perl
- 2. 附加转换UTF8字符串数组
- 3. 将字节[]转换为UTF8字符串
- 4. 将字符串转换为utf8字节
- 5. 将字符串转换成数值
- 6. 在Perl中将UTF8字符串转换为ASCII
- 7. 将utf8代码点字符串转换为utf8 <U+0161>转换为utf8
- 8. 转换字符串转换成int()
- 9. 转换日期/时间字符串转换成数值
- 10. 如何将字符串转换为UTF8?
- 11. 转换为UTF8格式的字符串
- 12. 将unicode字符串转换为utf8
- 13. Unicode转换为UTF8字符串
- 14. 将latin1字符串转换为utf8?
- 15. 转换JSON字符串UTF8为NSDictionary Swift
- 16. 将字符串转换为UTF8
- 17. 转换UINT8到字符UTF8
- 18. 拆分UTF8字符串转换成字符
- 19. 转换数字数组转换成字符串单元阵列
- 20. 转换的java字符串转换成JavaScript字符串
- 21. 字符串转换成Java
- 22. 转换时间戳字符串转换成数在甲骨文
- 23. 转换数组转换成字符串在AS3
- 24. 分裂在Perl字符串转换成数据库
- 25. 更改列值转换成字符串
- 26. 将字符串转换成波长/值
- 27. 哈斯克尔转换数字的字符串转换成
- 28. 转换dateTimePicker的值转换成字符串
- 29. 字符串数值转换在c#
- 30. 转换数字字符串转换为字符串
“中国的中国语言”?为什么'中国'? – Zaid 2010-08-22 20:19:40
我想它应该读*中文单词“中国”*。 – daxim 2010-08-23 09:39:42