Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing text file with split() 2

Status
Not open for further replies.

EchoCola

Programmer
Apr 13, 2004
48
US
Hello I want to parse a text file which will be comma delimited, it will look like this:
Code:
FOLDER1,SERVER,TO,xyz@abc.net
FOLDER1,SERVER,CC,xyz@abc.net,bruce@lee.com,bill@hotmail.com

Basically I want to put all this in an array and then be able to run tests because this will interface with an email program. I want to test for the FOLDER1, SERVER, and TO because I will need to know where who gets what email, about the email addresses there can be many. I tried to use the split() function the only problem is that I have to assign a variable to each field, I thought that the emails that didn't have a field defined for it would get appended to the last variable. THis is what I have so far, If i haven't been clear enough just ask..
thanks guys

Code:
open(fileIN, "emailconfig.txt")or die "Can Not Open Email File:$!\n";
@emailData = <fileIN>;
close(fileIN);

foreach $project (@emailData){

	chop($project);
	
	($one,$two) = split(/,/,$project);	
	
	print "One:$one  Two:$two  Three: $three \n";
}
 
[tt]split[/tt] has a third parameter which you might find useful to limit the number of fields returned. In your case, it looks like you might want either 3 or 4 and then supplied a result array that will hold 3 or 4 fileds.

Tom Morrison
 
Another option would be to do something similar to the following.

Code:
while (<DATA>) {
    my @record = split ",", $_;
    my ($folder, $server, $field) = splice @record, 0, 3;
    my $addys = join ",", @record;

    print "$folder $server $field $addys\n";
}

__DATA__
FOLDER1,SERVER,TO,xyz@abc.net
FOLDER1,SERVER,CC,xyz@abc.net,bruce@lee.com,bill@hotmail.com
 
Thanks, I used the version with splice, thank you, I didn't know about Splice() but now i DO! Thanks
 
Is there a character I can put preceding a line of text so that it will skip that line? kind of like a comment
 
There isn't a character that inherantly means "skip this line." But you can pick a character, or series of characters, and check a line for the presence of the character(s) prior to splitting all the fields. Be careful that you pick some pattern that shouldn't appear in the normal text.

Code:
my $skip = '#~';
my $len_skip = length($skip);

while (<DATA>) {
    unless (substr($_, 0, $len_skip) eq $skip) {
        my @record = split ",", $_;
        my ($folder, $server, $field) = splice @record, 0, 3;
        my $addys = join ",", @record;
        print "$folder $server $field\n\t$addys\n";
    }
}

__DATA__
#~ Skip this line
FOLDER1,SERVER,TO,xyz@abc.net
#~ Skip this line too
FOLDER1,SERVER,CC,xyz@abc.net,bruce@lee.com,bill@hotmail.com
 
I have an array that contains the following :
Code:
paul@compaq.com,thirteen@email.net,fourteen@email.net,fifteen@email.net,
What I wanted to do was to put single quotes around each email, still seperated by the commas. Basically I want it to look like:
Code:
'paul@compaq.com','thirteen@email.net','fourteen@email.net', etc..

I was thinking about using split() and then using join() but split only seems to work on scalars. Any ideas??
 
ok i got that working, by doing the following
Code:
chomp(@to,@cc);			 
@recipients = (@to,@cc);
@recipients = join ("",@recipients);
$recipients = @recipients[0];
@recipients = split (",",$recipients);
$recipients = join ("','",@recipients);
The only thing is with this i get the following output:
Code:
paul@compaq.com','thirteen@email.net','fourteen@email.net','fifteen@email.net

As you can see The first value and the last value does not contain a quote, how can i put it in there? So the first value would be

'paul@compaq.com' and the last would be 'fifteen@email.net'
 
$recipients="'".$recipients."'";

Neh
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
or even
Code:
$recipients="'$recipients'";

I hate waking in a cold sweat, it's why I dont sleep
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top