Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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 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.

By a scallop's forelocks!

In reply to Re: Capitalize the first letter of each word by Adrade
in thread Capitalize the first letter of each word by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2023-02-05 20:26 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (32 votes). Check out past polls.