0

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?

1
  • Consider what to do with Pebbles Flintstone Rubble Commented Apr 8, 2013 at 14:58

1 Answer 1

2
($fullname, $email) = split(/,/);
($first, @last) = split(/ /, $fullname);
print join(',', $first, "@last", $email), "\n";
Sign up to request clarification or add additional context in comments.

2 Comments

If the fullname has 3 or more words, e.g. "Anthony Michael Hall" or "J. R. R. Tolkien", this will not produce the right output.
Note: you can specify a limit for split, which would be a cleaner more efficient way to handle more than 2 word names: perldoc.perl.org/functions/split.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.