LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-20-2008, 09:52 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
Capital 1st letters in Perl


Hello,
If I have a string like "A pound of pigs", I'd like the P, the O and the P to be capital-ised , i.e. everything after a space. How would I do this in Perl?

THanks.
 
Old 08-20-2008, 10:04 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,399

Rep: Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958Reputation: 4958
Quote:
Originally Posted by resetreset View Post
Hello,
If I have a string like "A pound of pigs", I'd like the P, the O and the P to be capital-ised , i.e. everything after a space. How would I do this in Perl?

THanks.
Try this:

$string = "A pound of pigs";
$string =~ s/ ((^\w)|(\s\w)/\U$1/xg;
$string =~ /([\w']+)/\u\L$1/g;

print $string;

This will output "A Pound Of Pigs"

Take a look at some good docs on regular expressions.
 
Old 08-21-2008, 05:07 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
See perldoc -q capitalize for a fairly full discussion of options.
 
Old 08-21-2008, 01:41 PM   #4
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
You could always use ucfirst function instead. For example (added brackets for readability):

Code:
my $data = "there's more than one way to cap words!";
print join(' ', map({ ucfirst($_) } split('\s', $data) ) ),"\n";
Returns:

There's More Than One Way To Cap Words!

Hope this helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
capital letters on Fat32 Four Programming 2 07-21-2008 09:38 PM
e-mail to user name in capital letters y575 Linux - Networking 2 01-26-2007 01:49 AM
How to preserve capital letters? kosa Linux - General 1 07-14-2005 01:44 PM
usernames with capital letters in RH9 orange400 Linux - General 2 04-27-2004 05:57 AM
capital letters at the console alim Slackware 5 04-25-2002 01:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration