Gossamer Forum
Home : General : Perl Programming :

Splitting Up A Text File

Quote Reply
Splitting Up A Text File
  OK, this probably sounds really basic for you guys, but I just can't figure it out. I am making a mailing list, and I am going to include personalised messages. I am using "| |" as a delimeter between the email and the name, so the text file ends up looking like this.

something@somewhere.com| |Michael
you@your.com| |Samuel

I have to seperate the two so when I open the mailprog, and I can send out everyone a message, using a foreach statement.

SO far I have come up with this, not sure if I am even close.

sub getemails {
#Now we have all the addresses.
open(DATA, ">>$mainpath/emails.txt")| |
&error("The text files to store the email addresses does not exist.");
@emailaddress = <EMAILADDRESS>;
close(DATA);
@emailaddress = split (/&#0124; &#0124;/);
}

&getemails;

foreach $recip (@emailaddress) {
#Send A Message.
open (MAIL, "|$mailprog") &#0124; &#0124; die "Can't open $mailprog!\n";
print MAIL "To: $recip\n";

and so on with the mailprog etc which I know works.

Any help would be GREATLY appreciated! Thanks alot.
Quote Reply
Re: Splitting Up A Text File In reply to
Bit pissed now, so you'll excuse me if I'm inaccurate, but &#0124; &#0124; (which this forum, for some strange reason, decides to split with a space) is usually used as an OR statement in Perl. Better to use something else as a delimiter. Try using "~~", which is unlikely to come up.

adam

[SEE! Told you I was pissed!]

[This message has been edited by dahamsta (edited August 07, 1999).]
Quote Reply
Re: Splitting Up A Text File In reply to
  Thanks for the advice with not using &#0124; &#0124;, I didn't think that it would matter in a log file, but I'll change it anyway.

I still don't know how to split it up though, if the text file now looks like this.

someone@somewhere.com~~Michael
jake@somewhere.com~~Jake

Who do I split it??? I want to use a foreach as I have said before, so I will have to some how get the email and name in a seperate scalar.
Quote Reply
Re: Splitting Up A Text File In reply to
This will open the file, put the lines into an array, split each line in a foreach loop and print the result. You can do whatever you want with the data in the foreach loop, I'm just printing it for demonstration purposes.

Code:
open (FILE, $file) or die "Can't open $file";
@lines = <FILE>;
close(FILE);

foreach $line (@lines) {
($email, $name) = split("~~", $line);
print "$name - $email\n";
}

adam
Quote Reply
Re: Splitting Up A Text File In reply to
Here:
Code:
$message = qq~
This is just
a test message.
~;
$subject = "This is just a test subject.";
$youremail = "user\@server.com";
$mailprog = "/usr/sbin/sendmail";
open(EFILE, "/path/to/emails.txt");
@entries = <EFILE>;
close EFILE;
foreach $line (@entries)
{
@fields = split(/\|/,$line);
open (MAIL, "|$mailprog -t")
print MAIL "To: $fields[2]\n";
print MAIL "From: $youremail\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n\n";
close (MAIL);
}
have a nice day Smile

Pasha

------------------
The Flying Cranes, Inc:
http://www.theflyingcranes.com
Perl scripts & Web promotion:
http://find.virtualave.net
Quote Reply
Re: Splitting Up A Text File In reply to
Hey All, Im back,

here is the compleate code for it:

Code:

sub getemails {
#Now we have all the addresses.
open(DATA, ">>$mainpath/emails.txt")| |
&error("The text files to store the email addresses does not exist.");
@emailaddress = <EMAILADDRESS>;
close(DATA);
}

&getemails;

foreach $recip (@emailaddress) {
@email_data = split (/|/,$recip);
#Send A Message

$message = qq~
$email_data[1],
This is just
a test message.
~;
$subject = "This is just a test subject.";
$youremail = "user\@server.com";
$mailprog = "/usr/sbin/sendmail";
open (MAIL, "|$mailprog -t")
print MAIL "To: $email_data[0]\n";
print MAIL "From: $youremail\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n\n";
close (MAIL);
}

That is what I would use.

------------------
Sincerely,

Netoo! Support
Netoo! Internet Services Of America.
http://www.netoo.phiberoptix.com
Quote Reply
Re: Splitting Up A Text File In reply to
Sorry About the tabs and all, part of the code is to the right.

------------------
Sincerely,

Netoo! Support
Netoo! Internet Services Of America.
http://www.netoo.phiberoptix.com
Quote Reply
Re: Splitting Up A Text File In reply to
Um... is this alright?

#Now we have all the addresses.
sub getemails {
open (FILE, $file) or die "Can't open $file";
@lines = <FILE>;close(FILE);
foreach $line (@lines) {
($recip, $name) = split("~~", $line);
print "$name - $email\n";
}

&getemails;

#Send A Message
sub sendmessage {
$subject = "Example Subject";
$message = "testing";
open (MAIL, "|$mailprog -t")
print MAIL "To: $recip\n";
print MAIL "From: $adminemail\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n\n";
close (MAIL); }
}
&sendmessage;

[This message has been edited by Michael_Bray (edited August 09, 1999).]