Perl - UNIX 中与 Windows 不同的排序

发布于 2025-01-06 19:30:40 字数 204 浏览 6 评论 0原文

我在 Perl 中排序时遇到问题,在 Windows 和 Unix 中结果不同。

这些字符是: a - _ 1 2

In Windows: _ 1 2 - a
In Unix: _ - 1 2 a

我猜语言环境与此有关 - 我该怎么做才能使 Unix 排序与 Windows 排序匹配?

谢谢!

I'm having issues sorting in Perl having different results in Windows and Unix.

The characters are: a - _ 1 2

In Windows: _ 1 2 - a
In Unix: _ - 1 2 a

I'm guessing the locale has something to do with this - what can I do to make the Unix sort match the Windows sort?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我纯我任性 2025-01-13 19:30:40

文档说:

***警告***环境指定的区域设置会影响排序顺序。设置 LC_ALL=C 以获取使用本机字节值的传统排序顺序。

所以使用

LC_ALL=C sort ...

示例:

$ perl -E'say for @ARGV' a - _ 1 2 | LC_ALL=en_US.UTF-8 sort
_
-
1
2
a

$ perl -E'say for @ARGV' a - _ 1 2 | LC_ALL=C sort
-
1
2
_
a

The docs say:

*** WARNING *** The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.

so use

LC_ALL=C sort ...

Example:

$ perl -E'say for @ARGV' a - _ 1 2 | LC_ALL=en_US.UTF-8 sort
_
-
1
2
a

$ perl -E'say for @ARGV' a - _ 1 2 | LC_ALL=C sort
-
1
2
_
a
柠栀 2025-01-13 19:30:40

如果您不想使用区域设置,请注释掉包含的行,

use locale;

如果没有这样的行,Perl 中的 sort 在 Windows 和 Unix 上的行为应该相同。

您还可以在排序之前添加

no locale;

(或者更好的是,将排序包含在以其开头的块中)。

If you do not want to use locale, comment out the line containing

use locale;

Without such a line, sort in Perl should behave the same on both Windows and Unix.

You can also add

no locale;

before the sort (or, better, enclose the sort into a block starting with it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文