Solved

cases for multi-word strings

Posted on 2013-01-21
11
190 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
Independent Software Vendors: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do I grep for a specfic DNS record in a directory 3 49
Exchange 2010 Transport Rule Regex 28 131
Perl DBI Transactions Using Custom Module 7 48
Perl string filter 5 84
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…
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…

680 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