perl:两个字符串数组的差值



我想找到两个字符串数组的差异我不熟悉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'
);

这是一个使用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子(它使用mapgrep):

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

最新更新