【问题标题】:Searching a certain module for comparing arrays搜索某个模块以比较数组
【发布时间】:2011-12-17 15:17:40
【问题描述】:

有没有可以更好地做到这一点的模块(不仅仅是替换智能匹配部分)?

#!/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 );

【问题讨论】:

    标签: arrays perl comparison perl-module


    【解决方案1】:

    您正在寻找集合差异或者可能是相对补语,这个例子是模棱两可的。众多set modules 中的任何一个都足够了。

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

    【讨论】:

      【解决方案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";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-05
        • 1970-01-01
        • 1970-01-01
        • 2010-11-19
        • 2017-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多