1

感谢您的阅读。

目标:

  1. 我想将电子邮件地址 aa@gmail.com、bb@yahoo.com 从一个阵列转移到另一个阵列。

  2. 每次我都想用200个这个列表。

  3. 这些电子邮件必须来自不同的域名。但是列表已经按域名排序,(它是用 MAP 完成的)

  4. 所以更简单地说:它是关于通知循环遍历数组的指针,它必须在哪里获取电子邮件,并且每次它找到不同的域名时,最多会收到 200 封电子邮件。

所以,我想出了(为此道歉)损坏的伪代码,因为这是我今天第一次阅读关于 MAP 的内容,结果发现这个块有点复杂。

如何:

  @destination_list =

map {$_->[0]}  # map back

for(my $i = 1; $i<201; ++$i) # this is to do the round of 200 emails per day
if($_ != $1 ) # compares 2 domains 

{


shift(@oldlist); # extract one from the old list and send it to the new list



}


map { m/@([a-zA-Z0-9\-.]*)\b/; [$_, $1]} # this gets what the domain name is
  @oldlist

谢谢你

4

2 回答 2

3

通过始终首先从具有最多电子邮件地址的域中进行选择来最大限度地减少组的数量。

my %addrs_by_domain;
for my $addr (@addrs) {
   my $domain = ... extract domain of $addr ...;
   push @{ $addrs_by_domain{$domain} }, $addr;
}

while (%addrs_by_domain) {
   my @domains_by_freq =
      sort { @{ $addrs_by_domain{$b} } <=> @{ $addrs_by_domain{$a} }
       keys(%addrs_by_domain);

   splice @domains_by_freq, 200;

   my @group;
   for my $domain (@domains_by_freq) {
      push @group, shift( @{ $addrs_by_domain{$domain} } );
      delete( $addrs_by_domain{$domain} )
         if !@{ $addrs_by_domain{$domain} };
   }

   do_it(@group);
}
于 2012-07-01T18:13:38.903 回答
1

这是我们在聊天中提出的。它每次都处理整个列表并产生一个充满列表的数组引用,每天一个。可以提供想要的那一天,以及“在第 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
于 2012-07-02T11:01:18.500 回答