Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Capitalize the first letter of each word

by Anonymous Monk
on Jul 11, 2005 at 16:49 UTC ( #474011=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I convert "to say it once" to "To Say It Once"?

Retitled by davido from 'Capitalize the fist letter of each word'.

Comment on Capitalize the first letter of each word
Re: Capitalize the first letter of each word
by suaveant (Parson) on Jul 11, 2005 at 16:52 UTC
Re: Capitalize the first letter of each word
by davidrw (Prior) on Jul 11, 2005 at 16:54 UTC
    Are you trying w/regular expression approach or other? For non-re, lookup split, map, lc, ucfirst, and join in perldoc perlfunc. For re, you can a word boundry \b (see perldoc perlre) followed by a lower case letter and then uc it (look at the /e modifier).

    Update: sauveant beat me to the search for the recent thread
Re: Capitalize the first letter of each word
by gam3 (Curate) on Jul 11, 2005 at 17:17 UTC
    As I am not feeling pedantic today. Here is an answer.
    print(join' ', map({ ucfirst() } split /\s/, "to say it once"), "\n");
    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: Capitalize the first letter of each word
by Adrade (Pilgrim) on Jul 11, 2005 at 18:18 UTC
    Before I knew about ucfirst(), I wrote the following function to satisfy a specific need - you can use it as the basis for one of your own construction. It doesn't work like ucfirst though, as I needed to catch things like Mr. and Mrs. and have words one or two letters long left in their case... but you get the idea.

      -Adam

    --
    By a scallop's forelocks!

Re: Capitalize the first letter of each word
by injunjoel (Priest) on Jul 11, 2005 at 19:46 UTC
    Greetings all,
    Yet another way to do it.
    use strict; my $text = "to say it once"; $text =~ s/(\w+)/\u\L$1/g; print $text;
    produces:
    To Say It Once

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

      Of course, your solution is broken for the reasons I explain in perlfaq4.

      --
      brian d foy <brian@stonehenge.com>
        Thanks for the heads up.

        -InjunJoel
        "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
Re: Capitalize the first letter of each word
by tlm (Prior) on Jul 12, 2005 at 01:03 UTC
    $string =~ s/(?:^|(?<=\s))(\w)/\U$1/g;

    the lowliest monk

Re: Capitalize the first letter of each word
by radiantmatrix (Parson) on Jul 12, 2005 at 19:12 UTC

    Whee!

    my $start = 'to say it once'; print join ' ', map { ucfirst($_) } split '\s', $start;
    Larry Wall is Yoda: there is no try{}
    The Code that can be seen is not the true Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://474011]
Approved by davidrw
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-07-14 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (254 votes), past polls