[英]Convert UTF8 string into numeric values in Perl
例如,
my $str = '中國c'; # Chinese language of china
我想打印出數值
20013,22283,99
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
比其他分割快一點,因為它不構建代碼點列表。)
foreach my $c (split(//, $str))
{
print ord($c), "\n";
}
或者壓縮成一行: my @chars = map { ord } split //, $str;
Data :: Dumper ed,這會產生:
$VAR1 = [
20013,
22283,
99
];
要在源代碼中識別出use 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;
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.