本文介绍了如何从数组中减去数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试以下时

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

我得到一个空数组.我本来希望从 @a 中减去 @bl,所以输出是 yellow Purple pink.

I get an empty array. I would have expected that @bl was subtracted from @a, so the output was yellow purple pink.

这里出了什么问题?

推荐答案

需要将@bl转成hash来进行集差:

You need to turn @bl into a hash to perform the set difference:

my %in_bl = map {$_ => 1} @bl;
my @diff  = grep {not $in_bl{$_}} @a;

这篇关于如何从数组中减去数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 01:08