Need help with getting unique values in a file.

K

Koncept

Sorry for asking, but I give up on this situation. I am a total n00b at
Perl and have only used it for about 1 week now. I would really
appreciate somebody's help here because I am really feeling stuck.

I have a list of 1000's of email addresses in a file ( This is not a
spam list first and foremost ). I need to read that file in and create
unique lists sorting by domain. A particular domain must be listed once
per list. To make this a bit more clear....

If my addresses in the source file are as follows:

bill at one.com
jane at one.com
frank at two.com
ted at one.com
jess at three.com

My first run should return:
--------------------
bill at one.com
frank at two.com
jess at three.com

2nd run:
------
jane at one.com

3rd run:
------
ted at one.com

I came up with this to grep unique domains once, but the problem is
that the script only runs once.

#!/usr/bin/perl -w

sub div() { "+","-" x 50, "+\n"; }

die "Usage: $0 emailList" if (@ARGV!=1);

open( EML, $ARGV[0] ) || die "Can't open file : $!\n";

while(<EML>){
 chomp;
 push(@addys, $_) if $_ =~
/^[a-zA-Z0-9_\.\-]+\@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/;
}

close( EML );

foreach $email( @addys ) {
 @parts = split( "@", $email );
 $domain = $parts[1];
 unless( $seen{$domain} ) {
   push( @users, $email );
   $seen{$domain} = 1;
 }
}

if(@users>0){
 print &div, "The following are uniq users per domain:\n", &div;
 print join( "\n", sort( @users ) ), "\n", &div;
} else {
 print "Sorry. I could not find any email addresses.\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top