【问题标题】:Perl - deleting items of one array from another arrayPerl - 从另一个数组中删除一个数组的项目
【发布时间】:2013-10-12 23:04:26
【问题描述】:

我有两个名为 @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

我在这里错过了什么?

【问题讨论】:

标签: arrays perl


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2021-07-27
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多