当我尝试以下
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @bl = qw(red green blue);
my @a = qw(green yellow purple blue pink);
print Dumper [grep {not @bl} @a];
我得到一个空数组。我本来希望从
@bl
中减去@a
,所以输出是yellow purple pink
。怎么了
最佳答案
您需要将@bl
转换为哈希值以执行设置差异:
my %in_bl = map {$_ => 1} @bl;
my @diff = grep {not $in_bl{$_}} @a;
关于arrays - 如何从数组中减去数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4891898/