LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 01-10-2009, 10:42 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
How to capitalise ONLY the first letter in Perl


Hello,
First of all, apologies to anyone who wrote anything in the last thread I posted about this (if you aren't one of them, just ignore that).

I want to capitalise ONLY the first letter in a string that I have, eg: I want "a pound of pigs" to turn into "A Pound Of Pigs", but *also* "a pOuND oF PigS" to turn into "A Pound Of Pigs" - how do I do that?

Thanks.
 
Old 01-10-2009, 11:15 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Quote:
Originally Posted by resetreset View Post
Hello,
I want to capitalise ONLY the first letter in a string that I have, eg: I want "a pound of pigs" to turn into "A Pound Of Pigs",

Thanks.
$line =~ s/\b(\w)/\U$1/g;

Quote:
Originally Posted by resetreset View Post
but *also* "a pOuND oF PigS" to turn into "A Pound Of Pigs" - how do I do that?
$line =~ s/(\w+)/\u\L$1/g;

Note: I am not a perl programmer
 
Old 01-10-2009, 11:15 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Here's one way:
Code:
#!/usr/bin/env perl
use strict;
use warnings;

my $string = "a pound of pigs\n";
$string =~ s/(\w+)/\u\L$1/g;

print $string;
Check out the perl documentation on the string escapes \U, \u, \L, \l and the functions uc, lc, ucfirst and lcfirst.

Hah! MegaManX beat me to it, by just that much.
 
Old 01-10-2009, 11:19 AM   #4
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
LOL, what a timing Telemachos Our code matches, always a good sign, hehe
 
  


Reply



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
non-letter in a word malikah Programming 1 07-12-2008 09:05 PM
Script to move directories based on first letter to a new directory of that letter tworkemon Linux - Newbie 8 01-30-2007 07:18 PM
New letter from SCO UltimaGuy General 43 01-30-2004 10:09 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Letter........ nautilus_1987 General 5 12-25-2002 05:26 PM

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

All times are GMT -5. The time now is 01:22 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
Open Source Consulting | Domain Registration