Solved

cases for multi-word strings

Posted on 2013-01-21
11
188 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now