in reply to simpler regex

You didn't happen to mention... is it the case that the list of input parameters for your "fullname" function happen to be the space-separated tokens that make up the full name?

If that's what is being passed to the funcion, then it would be much simpler to add periods as needed before joining the parts together:

sub fullname { my @parts = @_; for ( @parts ) { $_ .= '.' if ( /^[A-Z]$/ ); } return join " ", @parts; }

Replies are listed 'Best First'.
Re^2: simpler regex
by rsiedl (Friar) on May 16, 2007 at 03:36 UTC
    That would be ideal, but i cant be guaranteed the user will input the data correctly. i.e. they may put in the middle name section "P J"...
      That's easy enough to accommodate:
      sub fullname { my @parts = @_; for ( @parts ) { s/(?<!\S)([A-Z])(?!\S)/$1./g; } return join " ", @parts; }
      Of course, given that sort of regex, I guess it doesn't matter whether you join the parts before or after the substitution. (It's using negative look-behind and negative look-ahead to check that a single upper-case letter is neither preceded nor following by a non-whitespace character, and in that case, put a period after the letter.)