我正在尝试计算文件中的电子邮件。 但似乎存在不能按预期工作。 当我发现新的电子邮件时,代码应该打印不是哈希 否则它应该以哈希打印。
但是“哈希”从不打印
我如何计算电子邮件的发生次数?
use strict;
use 5.014;
open(FCG,"<","pruecorreos") or die "No se puede \n";
my %correos = ();
my $i = 0;
while (<FCG>) {
chomp;
print "\nAnalizando: $_";
if ( my @m = $_ =~/(\w+@\w+\.\w+)(\.\w+)*/ ) {
my $lel = join("",@m);
print "lel es [$lel]";
if ( exists $correos{$lel} ) {
print "\n$lel in hash";
$correos{$lel}=1;
}
else {
print "\n$lel NOT in hash";
$correos{$lel}++;
}
}
}
答案 0 :(得分:2)
你错了。如果我针对此数据文件运行您的代码
aaa@mail.example.com
aaa@mail.example.com
然后我得到了这个输出
Analizando: aaa@mail.example.comlel es [aaa@mail.example.com]
aaa@mail.example.com NOT in hash
Analizando: aaa@mail.example.comlel es [aaa@mail.example.com]
aaa@mail.example.com in hash[Finished in 0.1s]
表示正在正确检测重复
但是,我在评论中指出的错误需要修复,我会这样做
use strict;
use warnings 'all';
open my $fcg, '<', 'pruecorreos' or die "No se puede: $!";
my %correos;
while ( <$fcg> ) {
chomp;
print "Analizando: $_\n";
next unless my ($lel) = /( \w+ \@ \w+ (?: \. \w+ )+ )/x;
print "lel es [$lel]\n";
print $correos{$lel}++ ? "$lel is in hash\n" : "$lel is NOT in hash\n";
print "\n";
}
Analizando: aaa@mail.example.com
lel es [aaa@mail.example.com]
aaa@mail.example.com is NOT in hash
Analizando: aaa@mail.example.com
lel es [aaa@mail.example.com]
aaa@mail.example.com is in hash