Perl - How do I count and print occurrences of domains in email address array? -
i have been struggling couple days , cannot seem figure out.
i have array of email addresses created via push(@emails,$email)
in while loop.
i attempting create list of unique domains occurrence count of each in array.
ordered number of occurrences.
so, if array @emails
has:
john@yadoo.com ringo@geemail.net george@zoohoo.org paul@yadoo.com
i can print:
yadoo.com 2 geemail.net 1 zoohoo.org 1
i found example based on emails in file but, way on head. can me in more verbose code example can used 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 tried: (more level of lack of understanding)
foreach $domain (sort keys %$domains) { print "$domain"."="; print $domains->{$domain}."\n"; };
and
my %countdoms; $countdoms{$_}++ @domains; print "$_ $countdoms{$_}\n" keys %countdoms;
the best result got of many different attempts total count (which 1812 (accurate count) number 2 next it. close, possibly?
if have email address populated in array this'll count each domain. i'm sure can produce prettier!
my @emails = ('john@yadoo.com','ringo@geemail.net','george@zoohoo.org','paul@yadoo.com'); %domaincount; foreach(@emails){ if ($_ =~ /@(\w+.*)/){ $domaincount{$1}++; } } $domain (sort { $domaincount{$b} <=> $domaincount{$a}} keys %domaincount ){ print "$domain - $domaincount{$domain}\n"; }
Comments
Post a Comment