从一个数组到另一个数组的Perl Map(更新简化了它)

时间:2012-07-01 15:22:51

标签: perl map

感谢您的阅读。

目标:

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

  2. 每次我想使用该列表时,有200个。

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

  4. 所以更简单一点:它是关于通知指针循环通过数组,它必须获取电子邮件,每次它找到一个不同的域名,最多200封电子邮件。

  5. 所以,我已经提出了(为此道歉)破解的伪代码,因为今天是我第一次阅读关于MAP的事情,结果发现这个块有点复杂。

    HOWTO:

      @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
    

    谢谢

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:1)

以下是我们在聊天中提出的内容。它每次处理整个列表并产生一个列表的ref-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