Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to do script to separate name and email domain ?

by anakin30 (Acolyte)
on Sep 07, 2010 at 00:04 UTC ( #859093=perlquestion: print w/ replies, xml ) Need Help??
anakin30 has asked for the wisdom of the Perl Monks concerning the following question:

hie everyone

Need your help, i have a file which consists list of email addresses as below example:-

  • jaja@email.com
  • kaka@myemail.com
  • baba@youremail.com
  • lala@email.com
  • I need a script to separate the name and email domian,and count the number of unique domain from the file

    Based on the file above, i need a script which can give below result:-

  • 2 email.com
  • 1 myemail.com
  • 1 youremail.com
  • thanks you in advanced

    Content restored by GrandFather

    Comment on How to do script to separate name and email domain ?
    Re: How to do script to separate name and email domain ?
    by GrandFather (Cardinal) on Sep 07, 2010 at 00:22 UTC

      What have you tried and where are you having trouble? What Perl books have your read? Have you looked in the Tutorials section here? Important words to watch out for are split, regex and hash.

      True laziness is hard work

        Just looking for some hint to start

    How to do script to separate name and email domain ?
    by ww (Bishop) on Sep 07, 2010 at 01:13 UTC
      Perhaps the problem is merely a language issue -- your post suggests to me that you are somewhat fluent in English, but not a native-speaker. If so, GrandFather's response will be adequate and very helpful. Follow his suggestions and, if you get stuck, come back with your code and a specific question.

      But if you are asking the Monks to write a script for you, you should address your request to a job site. This is not www.free-code-a-matic.com

      Hmmm... as i scribbled this, the OP's content (and its title) disappeared. For the information of those who follow, the original said:

      hie everyone

      Need your help, i have a file which consists list of email addresses as below example:-

      # jaja@email.com # kaka@myemail.com # baba@youremail.com # lala@email.com

      I need a script to separate the name and email domian,and count the number of unique domain from the file

      Based on the file above, i need a script which can give below result:-

      # 2 email.com # 1 myemail.com # 1 youremail.com

      thanks you in advanced

      anakin30:
      Please don't remove original content after posting a question or answer! It's counterproductive for all and rude as well!

      Update: Wow. I 'thunk' consider and GrandFather had the job done before I could finish doing so!

    Re: How to do script to separate name and email domain ?
    by Lady_Aleena (Chaplain) on Sep 07, 2010 at 03:19 UTC

      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

        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.

    Re: How to do script to separate name and email domain ?
    by pemungkah (Priest) on Sep 07, 2010 at 05:05 UTC
      "Count the number of unique" problems are common; in general they can be addressed with something like this:
      my %classification; # Scan and "bucket" the items for each $thing (@source_of_things) { # If the thing needs to be transformed, truncated, or pattern-matche +d, # do that here to get the unique part you're interested in. $classification{$unique_part_of_thing}++; } # Analyze and report. foreach $item (keys %classification) { print "$item found $classification{$item} time", ($classification{$item}==1 ? '' : "s"), "\n"; # Or whatever you need to do with each unique item and the number of # times it was found. }
      Note that your source of things can be anything: the keys of a hash, the lines read from a filehandle, items in an array. This is a good general pattern of scan-and-count-unique that wil help in a lot of contexts. Sorting into frequency of ocurrence, etc. left as an exercise to the reader.
    Re: How to do script to separate name and email domain ?
    by CountZero (Bishop) on Sep 07, 2010 at 06:18 UTC
      More compact, but essentially the same as the previous ones:
      use strict; use warnings; my %domains; $domains{(split /@/)[-1]}++ for <DATA>; print map "# $domains{$_} $_", sort keys %domains; __DATA__ jaja@email.com kaka@myemail.com baba@youremail.com lala@email.com
      Output:
      # 2 email.com # 1 myemail.com # 1 youremail.com

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlquestion [id://859093]
    Approved by GrandFather
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2014-09-21 16:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (172 votes), past polls