Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Capitalize the 1st letter of each word

by TASdvlper (Monk)
on Jan 15, 2004 at 19:46 UTC ( [id://321644]=perlquestion: print w/replies, xml ) Need Help??

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

All,

Say I have a array of words:

my @array = qw ( foo bar foobar barfoo 1 2 3 );
Is there a regex I can do to make sure each word starts with a capital letter ? So, my new array would be.
my @array1 = qw ( Foo Bar Foobar Barfoo 1 2 3 );
Thanks ....

janitored by ybiC: Corrected misspelling in title for better searching

Replies are listed 'Best First'.
Re: Capitalize the 1st letter of each word
by Zaxo (Archbishop) on Jan 15, 2004 at 20:06 UTC

    A regex is best suited to altering the array in place s/(\w)(\w*)/\U$1\E$2/ for @array; You can use a regex, but to make a new array with initial caps, ucfirst is handier, my @arrayl = map {ucfirst} @array; The regex can also be applied to a string with the /g flag

    $_ = 'foo bar foobar barfoo 1 2 3'; s/(\w)(\w*)/\U$1\E$2/g; print; # Foo Bar Foobar Barfoo 1 2 3

    After Compline,
    Zaxo

      An easier regex/substitution would be:
      my @array = qw/one Two three four five/; s/(.)/\u\L$1/ for @array;
Re: Capitalize the 1st letter of each word
by ysth (Canon) on Jan 15, 2004 at 19:56 UTC
    Not sure if you mean a regex to apply to your perl source to change the code or something to put in your code to change @array1.

    If the latter,

    my @array1 = map ucfirst, @array;
    does it; no regex necessary.
Re: Capitalize the 1st letter of each word
by atcroft (Abbot) on Jan 15, 2004 at 19:56 UTC

    If you hit the Library, under Perl Functions, Alphabetical, you'll find ucfirst() and lcfirst(), which will upper- or lower-case (respectively) the first character of a given word. So you could do something along the lines of:

    my @array = qw( foo bar foobar barfoo 1 2 3 ); foreach my $i (0 .. $#array) { $array[$i] = ucfirst($array[$i]); }

    I suspect a regex in this case is actually overkill.... HTH....

      Or even faster:
      foreach (@array) { ucfirst($_); }
      In this case, $_ will point to the actual element in the array.

        Unfortunately, that doesn't work

        my @words = qw[ the quick brown fox ]; foreach (@words) { ucfirst($_); }; Useless use of ucfirst in void context at ...

        It would have to be

        foreach (@words) { $_ = ucfirst($_); }

        Or, more simple

        $_ = ucfirst for @words; print for @words; The Quick Brown Fox

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Timing (and a little luck) are everything!

Re: Capitalize the 1st letter of each word
by KPeter0314 (Deacon) on Jan 15, 2004 at 19:52 UTC
    I remembered seeing this in perldoc (it covers the first letter only scenario too): How do I capitalize all the words on one line

    The exercise to parse through your array would be another issue.

    update: Don't know where my mind was but I forgot about ucfirst. Thanx ysth.

    -Kurt

Re: Capitalize the 1st letter of each word
by thelenm (Vicar) on Jan 15, 2004 at 19:56 UTC
    my @array = qw(foo bar foobar barfoo 1 2 3); my @caps = map "\u$_", @array; print "@caps\n";

    -- Mike

    --
    XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
    -- grantm, perldoc XML::Simpler

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://321644]
Approved by KPeter0314
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-20 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found