これが私たちがチャットで思いついたものです。リスト全体を毎回処理し、1日に1つずつ、リストでいっぱいのarray-refを生成します。希望する日を指定することも、「x日後にこのドメインを再度使用しない」ブラックリストを提供することもできます。
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 (finally, your map ;-))
next if
grep { /$domain/ }
map { exists $blacklist{$_} ? @{ $blacklist{$_} } : () } (0..$i);
next if exists $moved{$address}; # THIS line was missing
$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