如何比较Perl中的打包值?

我想在Perl中使用 pack()函数来编码一些数据.然后我想将我的打包结构与另一个打包结构进行比较.我想比较这个打包结构的字节值.

根据文档,cmp使用当前区域设置来确定如何比较字符串.但我不希望任何情报应用于比较.我想要最接近memcmp()的东西.显然我不能使用< =>比较我的打包对象,因为它们不是数字.

在Perl中比较打包字符串的最佳方法是什么?

旁注:我一直在阅读this article on efficient sorting in Perl,它注意到普通排序函数使用类似memcmp的算法来比较结构.我想知道如何在不使用sort的情况下实现这样的比较.

解决方法

禁用块的区域设置注意事项并像往常一样使用cmp:
sub mycmp {
  no locale;
  $_[0] cmp $_[1];
}

perlop文档提供

lt,le,ge,gt and cmp use the collation (sort) order specified by the current locale if use locale is in effect. See 07001.

然后在perllocale

The default behavior is restored with the no locale pragma,or upon reaching the end of block enclosing use locale.

例如,跑步

my($one,$two) = map pack("N",$_) => 1,2;
say mycmp($one,$two);
say mycmp($two,$one);

输出

-1
1

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...