在Perl中,有一个内置的方式来比较两个数组是否相等? [英] In Perl, is there a built in way to compare two arrays for equality?
问题描述
我有一个字符串的两个数组,我想比较平等:
I have two arrays of strings that I would like to compare for equality:
my @array1 = ("part1", "part2", "part3", "part4");
my @array2 = ("part1", "PART2", "part3", "part4");
有一个内置的方式比较数组像有标量?
我想:
Is there a built-in way to compare arrays like there is for scalars? I tried:
if (@array1 == @array2) {...}
,但它只是评价在标量上下文每个阵列,并且因此相比每个阵列的长度。
but it just evaluated each array in scalar context, and so compared the length of each array.
我可以推出自己的函数来做到这一点,但似乎这样的低级操作,应该有一个内置的方式做到这一点。有没有?
I can roll my own function to do it, but it seems like such a low-level operation that there should be a built-in way to do it. Is there?
编辑:可悲的是,我没有5.10+或可选组件的访问
sadly, I don't have access to 5.10+ or optional components.
推荐答案
有是新的智能匹配运营商一>:
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
my @x = (1, 2, 3);
my @y = qw(1 2 3);
say "[@x] and [@y] match" if @x ~~ @y;
::比较:
在内部比较器通过加入把两个数组转换为字符串,并使用比较字符串 EQ
。
Internally the comparator compares the two arrays by using join to turn both arrays into strings and comparing the strings using
eq
.
我想这是一个有效的方法,但只要我们使用字符串比较,我宁愿使用这样的:
I guess that is a valid method, but so long as we are using string comparisons, I would much rather use something like:
#!/usr/bin/perl
use strict;
use warnings;
use List::AllUtils qw( each_arrayref );
my @x = qw(1 2 3);
my @y = (1, 2, 3);
print "[@x] and [@y] match\n" if elementwise_eq( \(@x, @y) );
sub elementwise_eq {
my ($xref, $yref) = @_;
return unless @$xref == @$yref;
my $it = each_arrayref($xref, $yref);
while ( my ($x, $y) = $it->() ) {
return unless $x eq $y;
}
return 1;
}
如果你是比较数组很大,加入他们会做了很多工作,并消耗大量的内存比一个只是每个元素的一个比较。
If the arrays you are comparing are large, joining them is going to do a lot of work and consume a lot of memory than just comparing each element one by one.
更新:当然,人们应该测试这样的语句。简单的标准:
Update: Of course, one should test such statements. Simple benchmarks:
#!/usr/bin/perl
use strict;
use warnings;
use Array::Compare;
use Benchmark qw( cmpthese );
use List::AllUtils qw( each_arrayref );
my @x = 1 .. 1_000;
my @y = map { "$_" } 1 .. 1_000;
my $comp = Array::Compare->new;
cmpthese -5, {
iterator => sub { my $r = elementwise_eq(\(@x, @y)) },
array_comp => sub { my $r = $comp->compare(\(@x, @y)) },
};
这是最坏的情况,其中 elementwise_eq
要经过的每一个元素在两个数组1_000倍,它表明:
This is the worst case scenario where elementwise_eq
has to go through each and every element in both arrays 1_000 times and it shows:
Rate iterator array_comp
iterator 246/s -- -75%
array_comp 1002/s 308% --
在另一方面,最好的情况是:
On the other hand, the best case scenario is:
my @x = map { rand } 1 .. 1_000;
my @y = map { rand } 1 .. 1_000;
Rate array_comp iterator
array_comp 919/s -- -98%
iterator 52600/s 5622% --
迭代器
性能下降得很快,但是:
iterator
performance drops quite quickly, however:
my @x = 1 .. 20, map { rand } 1 .. 1_000;
my @y = 1 .. 20, map { rand } 1 .. 1_000;
Rate iterator array_comp
iterator 10014/s -- -23%
array_comp 13071/s 31% --
我没看内存利用率。
I did not look at memory utilization.
这篇关于在Perl中,有一个内置的方式来比较两个数组是否相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!