Lucas
Lucas

Reputation: 3

How to subtract an array from an array / Array minus Array - PERL

I have arrays like below and I can't find good solutions to make it works:

my @a = qw( A A B C C D D );

my @b = qw( A B C C D );

and as a result I would like to get: @a - @b = (A D)

Thanks for help!

Upvotes: 0

Views: 1149

Answers (2)

Jeef
Jeef

Reputation: 27275

You can find some code an explanation here: http://www.perlmonks.org/?node_id=2461

Upvotes: 0

ikegami
ikegami

Reputation: 386361

my %b;
++$b{$_} for @b;
grep { --$b{$_} < 0 } @a

Upvotes: 2

Related Questions