Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

parse email address

by BigGer (Novice)
on Apr 03, 2013 at 09:52 UTC ( [id://1026813]=perlquestion: print w/replies, xml ) Need Help??

BigGer has asked for the wisdom of the Perl Monks concerning the following question:

Hi Oh Wise Ones, I am looking for some perls of wisdom. I am searching a log file for email addresses ending in @tiles.ie The code I am using is giving me duplicates so I get gerry@tiles.ie printed to the output file 10 times. What I am trying to do is output each email address only once. My Code is :-

open(LOGFILE, "logs") or die("Oh Magoo !!! You've done it again."); my @entries = <LOGFILE>; close LOGFILE; open (FH, ">> log_report.txt") or die("Oh Magoo !!! You've done it aga +in."); # Purpose Print all email addresses ended with @tiles.ie $i = 0; while ($i != @entries) { while ($entries[$i] =~ /([A-Za-z0-9._-]+\@tiles.ie)/g) { print FH $1 . "\n"; } $i++; }
Thanks in advance for you help. G

Replies are listed 'Best First'.
Re: parse email address
by marto (Cardinal) on Apr 03, 2013 at 10:02 UTC

    You're not using open in a sane way, consider copying the examples from the documentation, checking and reporting on errors (the $! variable). You don't print every email address matching your criteria to a file, if it exists more than once you're going to have duplicates. To find only the unique email addresses you can use the List::MoreUtils module, or roll your own. A good discussion of this problem and in depth description of an alternate solution can be found in Unique values in an array in Perl.

      Thanks for the pint in the right direction. G

Re: parse email address
by hdb (Monsignor) on Apr 03, 2013 at 11:09 UTC

    use strict; use warnings; { local $/=''; my $entries = <DATA>; } my %mail; while ($entries =~ /([A-Za-z0-9._-]+\@tiles.ie)/g) { $mail{$1}++; # count ocurrences } print join "\n", keys %mail; __DATA__ ljkljkds lkjlkj@tiles.ie lkjljlk gerry@tiles.ie kljlkjlkjdfs ljklkjlk lkjljk gerry@tiles.ie lklkj lkjlkjlkjjjjjjjjj

      Thats Great thanks for your help. G

Re: parse email address
by Gangabass (Vicar) on Apr 04, 2013 at 01:05 UTC
    Parsing email address is not so trivial as you can think so I recommend to use Email::Address for this.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-03-28 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found