您的代码中的错误已经被指出,所以这里有另一个建议:使用Email::Address:
$ cat addresses
bob@gmail.com
bob@yahoo.com
bobette@springfield-amusement-park.com
bobbyMcBobberson@springfield-amusement-park.com
bahb@yahoo.com
bob @ yahoo.com
bob @ springfield-amusement-park.com
postmaster@hotmail.com
$ perl -MEmail::Address -lne 'for (Email::Address->parse($_)) { $bobs{$_->format}++ if $_->user =~ /bob/i } END { print for sort keys %bobs }' addresses
bob@gmail.com
bob@springfield-amusement-park.com
bob@yahoo.com
bobbyMcBobberson@springfield-amusement-park.com
bobette@springfield-amusement-park.com
您说您想“制作具有不同列表的文件”?Email::Address 也可以提供帮助:
while (<DATA>) {
for (Email::Address->parse($_)) {
push @{$categories{by_host}{$_->host}}, $_;
push @{$categories{bobs}}, $_ if $_->user =~ /bob/i
}
}
然后这将在以每个地址的主机名命名的文件中创建一个用户名列表:
for my $host (keys $categories{by_host}) {
open my $hf, '>', "hosts.$host" or die $!;
for (@{$categories{by_host}{$host}}) {
print {$hf} $_->user, "\n"
}
close $hf
}
所以,在最后一个列表上运行:
$ cat hosts.springfield-amusement-park.com
bobette
bobbyMcBobberson
bob
$ cat hosts.yahoo.com
bob
bahb
bob