Perl - 从另一个数组中删除一个数组的项目

时间:2013-10-12 09:28:37

标签: arrays perl

我有两个名为@these_conf_users @these_account_users的数组,如下所示:

adrian bruce malcolm

brom testwp sajay bruce ast domainte adrian rahul freddy onetwo

目的是从第二个数组中删除元素(如果它们存在于第一个数组中)。 Using this example,这就是我所做的:

my %sorter;
@sorter{ @these_conf_users } = ();
@tester = grep ! exists $sorter{$_}, @these_account_users;
print "@these_conf_users";
print "@these_account_users";
print "@tester";

但这并不排除重复的元素。这是输出:

adrian bruce malcolm
brom testwp sajay bruce ast domainte adrian rahul freddy onetwo
brom testwp sajay bruce ast domainte adrian rahul freddy onetwo

我在这里错过了什么?

1 个答案:

答案 0 :(得分:1)

您的代码运行良好:

use strict;
use warnings;

my @these_account_users = qw/ adrian bruce malcolm /;
my @these_conf_users = qw/ brom testwp sajay bruce ast domainte adrian rahul freddy onetwo /;

my %sorter;
@sorter{ @these_conf_users } = ();
my @tester = grep ! exists $sorter{$_}, @these_account_users;

print "@these_conf_users\n";
print "@these_account_users\n";
print "@tester\n";

<强>输出

brom testwp sajay bruce ast domainte adrian rahul freddy onetwo
adrian bruce malcolm
malcolm