views:

1638

answers:

3

I'm new to Perl and am working on a project for school and am stuck.

Input: A given text file containing email addresses delimited by a space, tab, ",", ";" or “:” [can be on separate lines].

I am trying to read in the email addresses and put them into an array. I am able to parse the data on one line however if there are line breaks or returns I only get the last element.

Can someone help me figure out how to take a list with each address on a separate line and parse them? I have read a bit on regex but need much more practice. Thanks.

open(EmailAddresses, "EmailAdressesCommaList.txt") || die "Can not open file $!";

# 
while (<EmailAddresses>)
{
    chomp;
    # Split the line into words
    @lines = split /[ ,;:\t\r\n(\t\r\n\s)+?]/;
}

foreach $value (@lines)
{
    print $value . "\n";
}
+7  A: 
open(EmailAddresses, "EmailAdressesCommaList.txt") || die "Can not open file $!";
while(<EmailAddresses>) {
    chomp;
    push @lines, split /[ ,;:\t\r\n(\t\r\n\s)+?]/;
}
foreach $value (@lines) {
    print $value . "\n";
}

i.e. the problem isn't your regex, it's that you're overwriting @lines each time through the loop.

chaos
I have the habit to declare the variables in the scope I want them in. Here, I would put "my @lines;" before the while loop. I think that the missing declaration would be caught by "use strict; use warnings;".
Svante
+1  A: 

As chaos pointed out, you should push onto the array, not overwrite it, but your regex is strange, too. It seems that you want to do:

/[ ,;:\t\r\n][\t\r\n\s]+/

However, I think that this will work as well:

/[,;:\s]+/
Svante
Yeah, it's not the healthiest regex ever.
chaos
A: 

Chaos is correct. If you are going to open the text file and process it again in the same program remember to clear the array.

@lines = ();
Tone