[英]Perl exists not working as expected
I'm trying to count the emails occurrences in a file. 我正在尝试计算文件中电子邮件的出现次数。 But it's seems that exists is not working as expected.
但是,似乎存在的工作不按预期进行。 When i found a new email the code should print not in hash Else it should print in hash.
当我找到一封新电子邮件时,该代码不应以散列方式打印,否则应以散列方式打印。
But "in hash" is never printed 但是永远不会打印“散列中”
How can i count the email ocurrences? 我如何计算电子邮件的出现次数?
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}++;
}
}
}
You are mistaken. 你误会了。 If I run your code against this data file
如果我针对此数据文件运行您的代码
aaa@mail.example.com
aaa@mail.example.com
then I get this output 然后我得到这个输出
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]
which shows that duplicates are being detected correctly 这表明已正确检测到重复项
However, the errors that I pointed out in my comment need to be fixed, and I would do it like this 但是,我在评论中指出的错误需要修复,我会这样做
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.