Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Capitalize First Letter of Each Word

by displeaser (Hermit)
on Jun 30, 2005 at 13:51 UTC ( [id://471313]=note: print w/replies, xml ) Need Help??


in reply to Capitalize First Letter of Each Word

Hi,
Here's another one.

my @string =
(
"THIS IS TESTING",
"JOE MARCONES",
"RESIDENTIAL MAINTENANCE COMPANY",
);

@string=map(lc, @string);
@string=map(ucfirst,@string);

#or @string=map(ucfirst, map(lc,@string));
print "@string\n";

Displeaser
  • Comment on Re: Capitalize First Letter of Each Word

Replies are listed 'Best First'.
Re^2: Capitalize First Letter of Each Word
by tlm (Prior) on Jun 30, 2005 at 14:07 UTC

    A single map will suffice:

    % perl -le 'print "@{ [ map ucfirst lc, qw( ODE TO PERL ) ] }"' Ode To Perl

    the lowliest monk

      Using map implies a join of some sort. This will normalise any spaces that may have been in your original data unless the regex used to split the data is zero width.
      my $new = join '', map {ucfirst lc} split /(?=\s)/;
      No maps are needed at all. Inputting one line at a time:
      $ perl -pe 's/([A-Z]+)/ucfirst(lc $1)/egi' "THIS IS TESTING" "This Is Testing" "JOE MARCONES" "Joe Marcones" "RESIDENTIAL MAINTENANCE COMPANY" "Residential Maintenance Company" THIS iS a Text IN TITLE CASE This Is A Text In Title Case This isn't right This Isn'T Right And I'm wrong too And I'M Wrong Too e-mail is nice when capped correctly E-Mail Is Nice When Capped Correctly &c &C "Bit o' Mint" "Mano y Mano" "Chris diBona" "Scrooge McDuck" "Bit O' Mint" "Mano Y Mano" "Chris Dibona" "Scrooge Mcduck" "Cruella de Ville" "Jake O'Shaunessy" "Tales from the Crypt" "Cruella De Ville" "Jake O'Shaunessy" "Tales From The Crypt" "Sanford and Son Salvage Company" "Sanford And Son Salvage Company"
      Of course this won't and can't handle all cases. But I usually use this for small amounts of text, either to TURN OFF THE SHOUTING or to convert an all-caps title to title case, and I'm willing to correct the few errors that it leaves or produces.
      Hi
      I kindof did in my commented out line, but my aim was to make it easier for the OP to read & hopefully understand what i was doing.

      Your code is neat though. I'll ++ it tomorrow when I have some more votes ;-)

      Displeaser

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://471313]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-18 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found