Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
No such thing as a small change
 
PerlMonks  

How do I store all matches from a single line?

by psmail (Initiate)
on Jun 05, 2001 at 17:38 UTC ( [id://85903]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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: pattern matching on a single line
by FouRPlaY (Monk) on Jun 05, 2001 at 17: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
Re: How do I store all matches from a single line?
by larryk (Friar) on Jun 05, 2001 at 21: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!"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://85903]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.