Like grep. I'd use split, splitting on whitespace.

This will give you a list (which may have one element). Since your list doesn't contain surnames, I'd filter the list grep to eliminate initials, except when their elimination leaves nothing (I know people who use initials in lieu of their given names).

I've got a sample here:

#!perl use strict; use warnings; my @names = ( 'J Q', 'G Gordon', 'Mary Jane', 'Tommy K', 'Madonna', 'George W.', 'Jacques-Yves'); foreach my $name (@names){ my @nl = split(/\s+/, $name); my @list = grep { ! /^[A-Z][^A-Z]*$/i} @nl; pop(@nl) if (@list and $#nl > 0 and $nl[-1] =~ /^[A-Za-z][^A-Za-z] +*$/); $name = join(' ', @nl); } print join("\n", @names) . "\n";
which produces this output:
J Q G Gordon Mary Jane Tommy Madonna George Jacques-Yves

I know it could be both better written and much shorter, but it's intended to be a demonstration, not production code.

emc

At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

—Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

In reply to Re: Parsing out first names by swampyankee
in thread Parsing out first names by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.