in reply to grabbing certain lines

Something like this should do the trick.

my %names_emails = map { /^(?:From|To): (\S+) (\S+)/ } @data;
You may have to change the two '(\S+)' in the regex to better match your specific data format,
but this should grab whatever is captured in the last two capturing paren groups and use those
as key value pairs to populate the hash.

Best Regards,
Wonko

Replies are listed 'Best First'.
Re: Re: grabbing certain lines
by FamousLongAgo (Friar) on Nov 11, 2002 at 17:57 UTC
    This method assumes you're pulling the entire file into @data.

    If you don't want to do that, filter the lines first:
    my @lines = grep { /^(From|To):/ } <FILE>;

      Well this is what I did
      if(/^(((From:)|(To:)|(Reply-To:)).+)/){ print "$1\n";
      This give me the whole line. i.e. From: misterbob <bob@mister.com> But now I want to get rid of the From:, To: or what ever is before the ":" and keep misterbob and <bob@moster.com>. I tried splitting on ":" but that didn't seem to work for me. Please advise. Thank you.
        I believe if you just change your code to:

        print $2 if ($text =~ /^(From|To|Reply-To):\s+(.+)/);

        That works for me in your test example.

        What you should be doing is looking for the .+ part of your regex and you're not saving that bit. You need to say (.+) and look at $2 and you should be fine. I also moved the : outside your or statement as there's no need that I know of to type that : 3 times when you can get away with once. :)

        Hope that helps!

        There is no emoticon for what I'm feeling now.

      I belive you'll find that still reads the entire file in before running the grep - so filtering the file first doesn't really give you anything.

      If the size of the file is an issue you'll want to use a loop to go over it line by line. Something like this should do it:

      my %name2email; while (<DATA>) { next unless m/^(?:To|From|Reply-To): (\S+)\s+<?([^\s>]+)/; $name2email{$1} = $2; };

      Note: the regex doesn't cope with a fair bit (multiple addresses, addresses without names, etc.).