使用 Perl 计算两个数组的差异 [英] Difference of Two Arrays Using Perl

查看:32
本文介绍了使用 Perl 计算两个数组的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组.我需要检查一个的元素是否出现在另一个中.

I have two arrays. I need to check and see if the elements of one appear in the other one.

有没有比嵌套循环更有效的方法?我每个都有几千个元素,需要经常运行程序.

Is there a more efficient way to do it than nested loops? I have a few thousand elements in each and need to run the program frequently.

推荐答案

另一种方法是使用 Array::Utils

use Array::Utils qw(:all);

my @a = qw( a b c d );
my @b = qw( c d e f );

# symmetric difference
my @diff = array_diff(@a, @b);

# intersection
my @isect = intersect(@a, @b);

# unique union
my @unique = unique(@a, @b);

# check if arrays contain same members
if ( !array_diff(@a, @b) ) {
        # do something
}

# get items from array @a that are not in array @b
my @minus = array_minus( @a, @b );

这篇关于使用 Perl 计算两个数组的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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