比较两个数组使用Perl [英] Comparing Two Arrays Using Perl

查看:596
本文介绍了比较两个数组使用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.

推荐答案

另一种方法是使用阵:: 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天全站免登陆