Solved

cases for multi-word strings

Posted on 2013-01-21
11
187 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
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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now