I am having a problem with a perl script.
Here is what it is supposed to do:
This is a script to combine first/last names and domain names to
create email addresses for employees. So it:
1)Parses a line from a CSV file
2)Rewrite the line, with 6 new fields at the end. (3 variations of
email addresses separated by blank cells.
For some reason it is consistently barfing when it gets to writing
line 17 of the names (out of a list of 50 records). It wrecks the
field alignment by adding a newline or something out of place. I can't
figure out why. Can someone help?
Code I am using posted here:
#!/usr/bin/perl
#insert files name below
my $file_in = 'small_test.csv';
my $file_out = 'email.csv';
# open files
open(ADDR, $file_in);
open(EMAIL, ">$file_out");
# deal with header line
my $line = <ADDR>;
print EMAIL ($line);
# process lines
$line = <ADDR>;
my $count = 1;
while ($line ne '') {
#check for quoted fields with commas
while($line =~ /(.*)?\"([\w|\s|\.]+)\,([\w|\s|\.\,]+)\"(.*)?/ ) {
$line =~ s/\"([\w|\s|.]+)\,([\w|\s|\.\,]+)\"/\"$1\|$2\"/ ;
}
my @values = split(/\,/, $line);
my $first = $values[0];
$first =~ s/\s//g;
$first =~ s/^(\w)+\.(.*)?/$2/;
my $initial = '';
if($first =~ /([\w])(.*)?/) {
$initial = $1;
}
my $last = $values[2];
$last =~ s/\s//g;
my $url = $values[26];
my $domain = '';
if($url =~ /^.*http:\/\/www.(.*)/i){
$domain = $1;
}
my $add1 = $initial.$last.'@'.$domain;
my $add2 = $first.'_'.$last.'@'.$domain;
my $add3 = $first.'.'.$last.'@'.$domain;
$values[27] = $add1;
$values[29] = $add2;
$values[31] = $add3;
my $newline = join(",", @values);
$newline =~ s/\|/\,/g;
print EMAIL ("$newline");
$line = <ADDR>;
my $rem = $count % 1000;
if (($count > 999) & ($rem == 0)) {
print "$count records converted.\n";
}
$count++;
}
close(ADDR);
close(EMAIL);
print "Conversion complete.\n"; |