in reply to How to do script to separate name and email domain ?

I just split the email addresses by the @ symbol, then pushed each user into an array that is keyed to the domain. After that I counted the values of each array in the hash and printed the results.

#!/usr/bin/perl use strict; use warnings; my %email_domains; for my $email_address (@email_list) { my ($before_at, $after_at) = split('@',$email_address); push @{$email_domains{$after_at}}, $before_at; } for my $domain (sort keys %email_domains) { my $count = @{$email_domains{$domain}}; print "$domain: $count"; }
Have a cookie and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^2: How to do script to separate name and email domain ?
by dasgar (Priest) on Sep 07, 2010 at 05:29 UTC

    anakin30, I would have done something similar to Lady_Aleena's approach, but I would have just stored the count in the hash (see code below). Of course, my approach wouldn't have stored all the email addresses for later use like her code does.

    use strict; my $file = "email_list.txt"; my %domains; open(DATA,"<",$file) || die "Unable to open file '$file': $!\n"; while (<DATA>) { my ($user,$domain) = split /\@/; $domain =~ s/^\s*//s; # Stripping out leading white spaces $domain =~ s/\s*$//s; # Stripping out trailing white spaces $domains{lc($domain)}++; } close(DATA); foreach my $domain (sort keys %domains) { print "$domains{$domain} $domain\n"; }

    Just another example of TIMTOWTDI.

    UPDATE - Made a minor spelling correction.