Need help with getting unique values in a file.

Discussion in 'Perl' started by Koncept, Apr 30, 2004.

  1. Koncept

    Koncept Guest

    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";
    }

    --
    Koncept <<
    "The snake that cannot shed its skin perishes. So do the spirits who are
    prevented from changing their opinions; they cease to be a spirit."
    -Nietzsche
     
    Koncept, Apr 30, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Muhammad Usman

    error when inserting unique values

    Muhammad Usman, Oct 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,103
    Srini
    Oct 16, 2003
  2. ToshiBoy
    Replies:
    6
    Views:
    868
    ToshiBoy
    Aug 12, 2008
  3. Drew Olson
    Replies:
    2
    Views:
    332
    William James
    Dec 19, 2006
  4. Paul
    Replies:
    3
    Views:
    106
  5. Token Type
    Replies:
    9
    Views:
    374
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page