http://qs1969.pair.com?node_id=474011

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Capitalize the first letter of each word

Replies are listed 'Best First'.
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 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 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 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