Solved

cases for multi-word strings

Posted on 2013-01-21
11
192 Views
Last Modified: 2013-02-19
# Setting the case of a last name is done like so:
$last = ucfirst lc("sampson");
gives "Sampson"

# What about setting case for a multi-word string? e.g.;
$last = ucfirst lc("sampson-smith, jr");
gives Samson-smith, jr

But how can I get "Sampson-Smith, Jr" , where each portion of the proper name is capitalized?
0
Comment
Question by:Marketing_Insists
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 125 total points
ID: 38803391
First thing that comes to mind is something like this but it doesn't quite work...

$last = join ' ', map { ucfirst $_ } split /-,\s\s*/, 'sampson-smith, jr';

A regex is probably what you want...

$last = 'sampson-smith, jr';
$last =~ s{\b(\w+)}{ucfirst $1}ge;
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 38803399
To explain the regex...
\b = zero-width word-break assertion
\w+ = as many word characters as possible

flags:
g = repeat as many times as possible
e = execute the code in the rhs of the substitution
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 125 total points
ID: 38803401
May be
my $last = "sampson-smith, jr";
$last =~ s/\b([a-z])/uc($1)/eg;
print $last;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
ID: 38803612
my $last = "sampson-smith, jr";
$last =~ s/\b(\w)/\u$1/g;  # or s/(\w+)/\u$1/g
print $last;
0
 
LVL 84

Expert Comment

by:ozo
ID: 38803856
First thing that comes to mind is something like this but it doesn't quite work...

$last = join ' ', map { ucfirst $_ } split /-,\s\s*/, 'sampson-smith, jr';
join '', map { ucfirst } split /(-|\s+)/, 'sampson-smith, jr';
#or
join '', map { ucfirst } split /([-,\s])/, 'sampson-smith, jr';
#or
join '', map { ucfirst } split /(?<=-|\s)/, 'sampson-smith, jr';  
#or
join '', map { ucfirst } split /(\W+)/, 'sampson-smith, jr';
#or
join '', map { ucfirst } split /\b/, 'sampson-smith, jr';  
#might have worked
0
 
LVL 19

Assisted Solution

by:Kim Ryan
Kim Ryan earned 125 total points
ID: 38812799
0
 

Author Comment

by:Marketing_Insists
ID: 38845857
Thanks
CPAN module may come in handy later with MacDonald vs Macer issues
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 38873503
4) For simplicity, I would simply divide the points evenly between the four contributors since all four answered the question accurately:
http:#a38803391
http:#a38803401
http:#a38803612
http:#a38812799
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question