On 10/7/10 Thu  Oct 7, 2010  12:20 PM, "jm" <jm5...@gmail.com> scribbled:


> 
> Shawn and John,
> 
> thanks, your leads gave me this:
> 
> #############################################
> #!/usr/bin/perl
> 
> print &surname($ARGV[0]) . "\n";
> 
> 
> # SUB SURNAME
> # removes leading/trailing whitespace
> # consolidates grouped whitespaces into single whitespaces
> # capitalizes first letter after "Mac/Mc/'" in name (names of
> Scottish/Irish descent)
> # capitalizes first letter of name upon return
> sub surname
> {
> my $name = shift;
> $name = join(' ', split(' ', $name));
> $name =~ s/(^[Mm]a?c|.')(.*)/\u$1\u$2/;
> return(ucfirst($name));
> } # end of sub surname
> #############################################
> 


> i'm thinking about trying to include the whitespace cleanup in the
> s/// but i'm thinking it'll be an ugly piece of code i'll always have
> trouble understanding.

Use a separate regex instead of the join/split:

    $name =~ s/\s+/ /g;

Not ugly. Easy to understand: "substitute any substring of one or more
whitespace characters with a single space character".

Don't try to add this to your other regex. I am not sure that can even be
done. I am sure that it is not worth it.

Here is one perhaps more specific to your problem that may be a little
harder to understand:

    $name =~ s/ {2,}/ /g;

That one will not substitute a single space with a single space, but you are
not likely to notice the difference in execution speed (if there even is
one). \s includes spaces, tabs, and newlines, so they are not exactly
equivalent.

Other possibilities:

    $name =~ s/\s{2,}/ /g;
    $name =~ s/[ ]{2,}/ /g;
    $name =~ s/\s\s+/ /g;



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to