【问题标题】:perl : difference of 2 arrays of STRINGS [duplicate]perl:2个字符串数组的差异[重复]
【发布时间】:2014-10-07 16:21:26
【问题描述】:

我想找出 2 个字符串数组的差异 我不熟悉 GREP 和 MAP 的使用。所以,我需要一点指导

这就是我想要做的......

my @array = (    
  'hello this is a text',
  'this is a cat',
  'this is a dog',
  'this is a person',
  'this is a computer',
  'this is a code',
  'this is an array',
  'this is an element',
  'this is a number'
);

my @array2 = (
 'hello this is a text',
 'this is a computer', 
 'this is an array',
);

my @output = (
  'this is a cat',
  'this is a dog',
  'this is a person',
  'this is a code',
  'this is an element',
  'this is a number'
);

【问题讨论】:

    标签: arrays perl map


    【解决方案1】:

    这是使用Array::Utils CPAN 模块的简单任务:

    use strict;
    use warnings;
    use Array::Utils qw/array_minus/;
    
    my @a = ( 
    # the data ...
    );
    
    my @b = (
    # the data ...
    );
    
    # get items from array @a that are not in array @b
    my @minus = array_minus( @a, @b );
    

    如果您不想安装该模块,只需复制/查看array_minus sub(它使用map 和grep):

    sub array_minus(\@\@) {
       my %e = map{ $_ => undef } @{$_[1]};
       return grep( ! exists( $e{$_} ), @{$_[0]} );
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多