在Perl中,有一个内置的方法来比较两个数组的平等? [英] In Perl, is there a built in way to compare two arrays for equality?

查看:235
本文介绍了在Perl中,有一个内置的方法来比较两个数组的平等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串数组,我想比较为相等:

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;

关于 Array :: Compare


比较器在内部比较两个数组使用 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%         --

我没有看内存利用率。

这篇关于在Perl中,有一个内置的方法来比较两个数组的平等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆