搜索某个模块以比较数组

时间:2011-12-17 15:17:41

标签: arrays perl comparison perl-module

是否有一个模块可以更好地完成这项工作(不仅仅是替换智能匹配部分)?

#!/usr/bin/env perl
use warnings;
use 5.014;

my @array_all = ( qw( one two three four ) );
my @array_part = ( qw( two four six ) );

my @temp;
for my $i ( @array_part ) {
    push @temp, $i if not $i ~~ @array_all;
}

# if ( @temp ) { do something );

2 个答案:

答案 0 :(得分:3)

您正在寻找设置差异或者相对补充,示例不明确。 <{3}}中的任何一个都足够了。

use Set::Object qw();
Set::Object
    ->new(qw(two four six))
    ->difference(Set::Object->new(qw(one two three four)))
    ->members;    # ('six')

答案 1 :(得分:2)

您可以使用List::Compare

#!/usr/bin/env perl
use strict;
use warnings;
use List::Compare;
my @array_all  = ( qw( one two three four ) );
my @array_part = ( qw( two four six ) );
my @temp;
my $lc = List::Compare->new('--unsorted',\@array_all,\@array_part);
@temp  = $lc->get_complement;
print "@temp\n";