Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

parse email address

by BigGer (Novice)
on Apr 03, 2013 at 09:52 UTC ( #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 The code I am using is giving me duplicates so I get 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 $i = 0; while ($i != @entries) { while ($entries[$i] =~ /([A-Za-z0-9._-]+\ { print FH $1 . "\n"; } $i++; }
Thanks in advance for you help. G

Replies are listed 'Best First'.
Re: parse email address
by marto (Bishop) 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 (Prior) on Apr 03, 2013 at 11:09 UTC

    use strict; use warnings; { local $/=''; my $entries = <DATA>; } my %mail; while ($entries =~ /([A-Za-z0-9._-]+\ { $mail{$1}++; # count ocurrences } print join "\n", keys %mail; __DATA__ ljkljkds lkjljlk kljlkjlkjdfs ljklkjlk lkjljk 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.