On Thu, Oct 7, 2010 at 3:01 PM, Jim Gibson <jimsgib...@gmail.com> wrote: > 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/ > > >
jim, thanks. i'd initially considered separate regex's for whitespace but decided on the join because it takes care of all whitespace (leading/trailing/embedded) in one fell swoop. i won't be trying to combine the join with the existing regex; decided i'm not that much of a glutton for punishment. i actually did understand the {2,} so maybe i'm not as far out in the cold as i'd feared :) . i appreciate your insights and suggestions. joe -- since this is a gmail account, please verify the mailing list is included in the reply to addresses -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/