perl:2个STRINGS阵列的差异

时间:2014-08-14 05:49:57

标签: arrays perl map

我想找到2个STRINGS阵列的区别 我是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'
);

1 个答案:

答案 0 :(得分:2)

使用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]} );
}