in reply to Newbie parsing problem

You may be able to use a Regular Expression for this, such as:
my ($last_name, $first_name, $middle_initial) = ( $_ =~ /(\w+)\W*?(\w+ +)\s?(\w)?/) ;
Here's the subroutine using the regexp you can plug right into liverpole's solution.
sub parse_name { my ($last, $first, $mi) = ( $_[0] =~ /(\w+)\W*?(\w+)\s?(\w)?/) ; return [$first, $last, $mi]; }
Updated: Noticed I missed the first no middle-initial case, oops!

Replies are listed 'Best First'.
Re^2: Newbie parsing problem
by mk. (Friar) on Jan 25, 2007 at 16:42 UTC
    your solution ignores the name in case there's no middle initial (or just no trailing spaces), which is the first example. adding \W* or \s* instead of a single blank space fixes this.
    /(\w+)\W*?(\w+)\W*(\w)?/

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    *women.pm
      Thanks, I noticed that, too, when I changed it to plug into the test case. Fairly embarrasing considering I do name parsing such as this regularly at work!
        great solutions, I have noticed that:
        my ($last, $first, $middle) = ($name =~ /(\w+)\W*?(\w+)\W*(\w)?/);
        doesn't handle lastnames with hypens in them, any ideas? thanks again!