【问题标题】:Confused about multilingual collation in Perl对 Perl 中的多语言排序感到困惑
【发布时间】:2014-09-07 17:37:06
【问题描述】:

就在我以为我理解 Perl 中的多语言排序规则的时候......

对于 $ 和 % 的相对排序,我得到了不同的结果 当我使用 Unicode 排序规则时,与我使用普通的旧“cmp”相比。我不认为我的 Unicode 结果是正确的。

以下脚本输出:

Vanilla collation: alpha CMP beta for  is -1      # I get it: "alpha" comes before "beta"
Unicode::Collate collation: alpha CMP beta is -1  # I get it: "alpha" comes before "beta"
Vanilla collation: $ CMP % for  is -1             # I get it $ comes before %
Unicode::Collate collation: $ CMP % is 1          # So why is this result different?

这就是我的问题:为什么 Unicode 排序规则认为 % 在 $ 之前?

use HTML::Entities;
use Unicode::Collate::Locale;

require 'Unicode/Collate/Locale/fr.pl';


my $COLL = Unicode::Collate::Locale->new(
    locale => "fr",
                     );

{
my $a_text = "alpha";
my $b_text = "beta";


my $result = $a_text cmp $b_text;    # returns 1, 0, or -1.
printf qq(Vanilla collation: %s CMP %s for $lang is %s\n), $a_text, $b_text, $result;

my $result1 = $COLL->cmp( $a_text, $b_text );    # returns 1, 0, or -1.
printf qq(Unicode::Collate collation: %s CMP %s is %s\n), $a_text, $b_text, $result1;
}

{
my $a_text = "\$";
my $b_text = "%";


my $result = $a_text cmp $b_text;    # returns 1, 0, or -1.
printf qq(Vanilla collation: %s CMP %s for $lang is %s\n), $a_text, $b_text, $result;

my $result1 = $COLL->cmp( $a_text, $b_text );    # returns 1, 0, or -1.
printf qq(Unicode::Collate collation: %s CMP %s is %s\n), $a_text, $b_text, $result1;
}

【问题讨论】:

    标签: perl unicode multilingual collation


    【解决方案1】:

    结果是正确的。

    Unicode Collation Algorithm 定义了事物的排序方式,在 mappings 的帮助下,包括以下内容:

    0024  ; [.15BA.0020.0002] # DOLLAR SIGN
    0025  ; [*037A.0020.0002] # PERCENT SIGN
    

    【讨论】:

    • 非常感谢您。 DOLLAR 符号的 ASCII 码是 36,% 的 ASCII 码是 37。所以我不得不得出结论,这两个字符的 ASCII 排序不同于 Unicode 排序。我以为他们不会互相矛盾。
    • ASCII 没有定义排序顺序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多