我想我已经解决了它,以“Konerak”的评论为出发点。事实上,这个问题是一个从未减少的清单。因为我对参考数组不了解,所以我有点迷茫,但是以某种方式阅读了我试图在预期行为中找到相似性的代码。
因此,我创建了另一个名为 @reserva 的数组并写了这个:
push @ {$reserva [$i]}, $address
代替
push @addresses, $address;
现在,无论我输入多少封电子邮件,我都会得到所需大小的列表。我尝试了 1000 次,不到一秒钟就没有问题。
所以,这是完整的代码
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my $only_index = 3; # Read from command line with $ARGV[0] or use Getopt::Long
my %blacklist = ( # Each key in this hash represents one index/day
'2' => [ 'a', 'b' ], # and has an arrayref of domains that have replied on
'3' => [ 'c' ], # that day. We look at all keys smaller than the current
); # index in each iteration and ignore all these domains
my @domains; # holds the domains we have already seen for each list
my @lists = ([]); # Holds all the lists
my %moved; # the addresses we moved to the back
my $i = 0;
my @addresses = <DATA>;
while (@addresses) {
my $address = shift @addresses;
chomp $address;
$address =~ m/@([a-zA-Z0-9\-.]*)\b/;
my $domain = $1;
# If the domain has answered, do not do it again
next if
grep { /$domain/ }
map { exists $blacklist{$_} ? @{ $blacklist{$_} } : () } (0..$i);
$i++ if (@{ $lists[$i] } == 2
|| (exists $moved{$address} && @addresses < 1));
if (exists $domains[$i]->{$domain}) {
push @addresses, $address;
$moved{$address}++;
# say "pushing $address to moved"; # debug
} else {
$domains[$i]->{$domain}++;
# send the email
# say "added $address to $i"; # debug
push @{ $lists[$i] }, $address;
}
}
# print Dumper \@lists; # Show all lists
print Dumper $lists[$only_index]; # Only show the selected list
1;
__DATA__
1@a
2@a
3@a
1@b
2@b
1@c
2@c
3@c
1@d
2@d
3@d
4@d
1@e
1@f
1@g
1@h
4@a
5@a
4@c