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


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!