0

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

adrian bruce malcolm

brom testwp sajay bruce ast domainte adrian rahul freddy onetwo

目的是从第二个数组中删除元素,如果它们存在于第一个数组中。使用这个例子,这就是我所做的:

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

我在这里错过了什么?

4

1 回答 1

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
于 2013-10-12T15:04:26.437 回答