FunLovinCoder
FunLovinCoder

Reputation: 7897

Create new CSV from existing CSV

I have managed to extract name and email addresses columns from from a CSV.

The email field is the only field that is always there. The name field can be blank, just a first name and sometimes a first name and last name. Like this:

Name, Email
Fred, [email protected]
Wilma Flinstone, wima@flintstones
,[email protected]

What I would like to do is create a new CSV from the above that splits the name into first and last if possible. So the above would look like this:

First Name, Last Name, Email
Fred, ,[email protected]
Wilma, Flinstone, wima@flintstones
,,[email protected]

How could I do this in Perl?

Upvotes: 0

Views: 82

Answers (1)

devnull
devnull

Reputation: 123688

($fullname, $email) = split(/,/);
($first, @last) = split(/ /, $fullname);
print join(',', $first, "@last", $email), "\n";

Upvotes: 2

Related Questions