Search code examples
arraysperlcountunique

Perl - How do I count and print occurrences of domains in email address array?


I have been struggling with this for a couple days now and cannot seem to figure it out.

I have an array of email addresses that were created via push(@emails,$email) in a while loop.

I am attempting to create a list of unique domains with occurrence count of each in the array.

Ordered by number of occurrences.

So, if the array @emails has:

[email protected] [email protected] [email protected] [email protected]

I can print:

yadoo.com 2
geemail.net 1
zoohoo.org 1

I found this example based on emails in a file but, WAY over my head. Can someone help me in a more verbose code example that can be used with an array of email addresses?

perl -e 'while(<>){chomp;/^[^@]+@([^@]+)$/;$h{$1}++;}
foreach $k (sort { $h{$b} <=> $h{$a} } keys %h)  {print $h{$k}." ".$k."\n";} infile

I also tried: (more to my level of lack of understanding)

foreach my $domain (sort keys %$domains) {
  print "$domain"."=";
  print $domains->{$domain}."\n";
};

AND

my %countdoms;
$countdoms{$_}++ for @domains;
print "$_ $countdoms{$_}\n" for keys %countdoms;

The best result I got of many different attempts was a total count (which was 1812 (accurate count) with a number 2 next to it. I am close, possibly?


Solution

  • If you have your email address populated in an array this'll get you a count for each domain. I'm sure someone can produce something prettier!

    my @emails = ('[email protected]','[email protected]','[email protected]','[email protected]');
    
    my %domainCount;
    
    foreach(@emails){
        if ($_ =~ /@(\w+.*)/){
            $domainCount{$1}++;
        }
    }
    
    for my $domain (sort { $domainCount{$b} <=> $domainCount{$a}} keys %domainCount ){
        print "$domain - $domainCount{$domain}\n";
    }