Reputation: 9
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}++;
}
}
}
Upvotes: 0
Views: 165
Reputation: 126762
You are mistaken. If I run your code against this data file
[email protected]
[email protected]
then I get this output
Analizando: [email protected] es [[email protected]]
[email protected] NOT in hash
Analizando: [email protected] es [[email protected]]
[email protected] 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: [email protected]
lel es [[email protected]]
[email protected] is NOT in hash
Analizando: [email protected]
lel es [[email protected]]
[email protected] is in hash
Upvotes: 2