Google Answers Logo
View Question
 
Q: Problem with a Perl Script ( No Answer,   1 Comment )
Question  
Subject: Problem with a Perl Script
Category: Computers > Programming
Asked by: iamchmod-ga
List Price: $25.00
Posted: 06 Feb 2003 21:14 PST
Expires: 06 Feb 2003 22:44 PST
Question ID: 158355
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";

Clarification of Question by iamchmod-ga on 06 Feb 2003 22:07 PST
the file is posted as a csv here
http://www.wherepc.com/wherepc-com.csv - all identifiers have been
stripped

Clarification of Question by iamchmod-ga on 06 Feb 2003 22:12 PST
for some reason it is adding a ^M after record 17 and then after the
resulting email combinations, messing up the formatting
Answer  
There is no answer at this time.

Comments  
Subject: Re: Problem with a Perl Script
From: scttyb-ga on 06 Feb 2003 21:51 PST
 
Could you provide a sample data file?  Of course change the names and
domains, but leave the formatting exactly the same.  Also - you should
be aware of names containing special characters like quotes or dashes.

Thanks.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy