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

Hi, I have a file in format:
...... aa<aa@aa.com>bb<bb@bb.com>cc<cc@cc.com> dd<dd@dd.dd>ee<ee@ee.com>ff<ff@ff.com> ......
I need to extract the email addresses between the characters < & > into a array. How do I do it..
Thanks

Replies are listed 'Best First'.
Re: How do I store all matches from a single line?
by larryk (Friar) on Jun 06, 2001 at 01:23 UTC
    Well I can think of a couple of things you could do depending on whether you want an array or a hash...
    # array my @emails = (); while (<DATA>) { push @emails, (split /[<>]/)[1,3,5]; }
    I don't like that though -- I'll qualify that -- hard-coding the indices seems like a bad idea, eg. if the data were to change to 4 sets of name/email on a line then this breaks. It's only a quick fix to get it working again but I still don't like it - call me fussy. I suggest the following (again, doesn't offer dodgy-data handling -- but will take as many on a line as are provided)...
    # hash undef $/; my %emails = split /[<>\n]+/, <DATA>;
    The hash-way means you don't lose the name info although, since names are not unique and email addresses are, you should maybe use the email as the key and the name as the value! I'll leave that for you to work out. Hope this helps, larryk

    "Argument is futile - you will be ignorralated!"

Re: pattern matching on a single line
by FouRPlaY (Monk) on Jun 05, 2001 at 21:42 UTC
    I'd try something like: @array = /<(\w+\@\w+\.\w+)>/g;
      That's incorrect for actual email addresses. If you just want all the angled parts of all the lines, it's something as simple as:
      @result = map /<.*?>/g, @lines;

      -- Randal L. Schwartz, Perl hacker

        I thought @second = map EXPR, @first; applied EXPR to each element of @first and placed the results in @second. And since m// doesn't change anything, then @second would equal @first.

        So something like:

        for (@lines) { @result = /<(.*?)>/g; }


        Would be more appropriate.

        However, I'm willing to bet heavily that your Perl knowledge far exceeds mine, so I'm wondering: do I misunderstand map or m// (or both)?



        FouRPlaY
        Learning Perl or Going To die() Trying