Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Capitalize the first letter of each word

by Adrade (Pilgrim)
on Jul 11, 2005 at 18:18 UTC ( [id://474045]=note: print w/replies, xml ) Need Help??


in reply to Capitalize the first letter of each word

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.

sub reform { my $line = shift; my @toret; foreach my $word (split(/\b/,$line)) { if (lc($word) eq 'mrs') { $word = "Mrs."; } elsif (length($word) > 2) { $word =~ s/^(.)(.*)/uc($1).lc($2)/se; } elsif (lc($word) eq 'mr') { $word = "Mr."; } push (@toret, $word); } return join("",@toret); }

adrade@antigone:~$ perl -e 'print ucfirst("il"),"\n"' Il adrade@antigone:~$ perl -e 'print ucfirst("HELLO"),"\n"' HELLO adrade@antigone:~$ perl -e 'print ucfirst("mrs"),"\n"' Mrs # I put reform() in ref.pm adrade@antigone:~$ perl -e 'use ref; print reform("il"),"\n"' il adrade@antigone:~$ perl -e 'use ref; print reform("HELLO"),"\n"' Hello adrade@antigone:~$ perl -e 'use ref; print reform("Mrs"),"\n"' Mrs.
  -Adam

--
By a scallop's forelocks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-03-28 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found