----- Original Message -----
From: "Curt Shaffer" <[EMAIL PROTECTED]>
To: <beginners@perl.org>
Sent: Friday, March 10, 2006 2:36 AM
Subject: pushing csv vaules into hash
I am really stuck here. I need to split values from a csv value and push
them into an array, then perform a routine for all of them in a foreach
statement. In this example I am reading an email address, a username and a
password and needing to send each user listed in the csv a mail to the
email
address with the message containing their username and password. I am
successful in reading the lines of the file and the ability to iterate
through them with once they are in the array, but then how do I get them
into variables to be used by the sub? Again in this example, I need to
create a variable ($to) from the first value, a username ($username) from
the second value and a password($password) from the third value. Those
variables will obviously be different for each iteration through the
foreach.
The package you are looking for Text::xSV
http://search.cpan.org/~tilly/Text-xSV-0.15/lib/Text/xSV.pm
does exactly what you want, takes a csv file and fits it into a hash
structure.
####code##############
#!/usr/bin/perl
use strict;
use Mail::Sendmail;
my $csvfile = 'test.csv';
my $currentline;
open (IN, "<$csvfile") or die "Couldn't open input CSV file: $!";
my @data = sort <IN>;
my $lastline = ' ';
foreach $currentline(@data){
next if $currentline eq $lastline;
my @split = split(',',$currentline);
foreach my $split (@split){
&MailPass;
}
}
close IN;
sub MailPass{
my $to = "";
my $username = "";
my $password = "";
my $from = "[EMAIL PROTECTED]";
my $subject = "Your Login Information";
my %mail = (To => $to,
From => $from,
Subject => $subject
Message => "Your username is $username and your
password is $password",
);
sendmail(%mail) or die $Mail::Sendmail::error;
}
############end code########################
Thanks for the help!
Curt
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>