Capitalizing words in a line. 
Author Message
 Capitalizing words in a line.

My own thanks to the fellow who needed to UPPERCASE certain key words in
a file.  It's a problem I just needed a solution for.  However...

My conumdrum? I need to *capitalize* some or all words on an input line.
That is, change from "lower case" to "Mixed Case And Capitalized Words"
(as in GnuEmacs's 'capitalize-word/region').

I tried doing a s|\w.|tr[a-z][A-Z]| and some variants, but nothing worked
correctly.  I plead to the perl community for help.

Please e-mail solutions and I will summarize for the net.

        -Peter "scalloped perl" Marvit

: Peter Marvit   Hewlett-Packard Labs in Palo Alto, CA   (415) 857-6646    :



Mon, 02 Aug 1993 08:09:35 GMT  
 Capitalizing words in a line.

PM> My own thanks to the fellow who needed to UPPERCASE certain key words in
PM> a file.  It's a problem I just needed a solution for.  However...

PM> I tried doing a s|\w.|tr[a-z][A-Z]| and some variants, but nothing worked
PM> correctly.  I plead to the perl community for help.

I thought the following might be a good try:

$_ = "this is a test case OF UPPERCASE\n";

s|\b(.)|$1=~tr/a-z/A-Z/|e;

print $_

However, perl dumps core (segmentation fault) on this. Looks like a bug to
me (or otherwise I should get an errormessage)
--
Piet* van Oostrum, Dept of Computer Science, Utrecht University,
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands.
Telephone: +31 30 531806   Uucp:   uunet!mcsun!ruuinf!piet



Tue, 03 Aug 1993 17:51:52 GMT  
 Capitalizing words in a line.


:
:PM> My own thanks to the fellow who needed to UPPERCASE certain key words in
:PM> a file.  It's a problem I just needed a solution for.  However...
:
:PM> I tried doing a s|\w.|tr[a-z][A-Z]| and some variants, but nothing worked
:PM> correctly.  I plead to the perl community for help.
:
:I thought the following might be a good try:
:
:$_ = "this is a test case OF UPPERCASE\n";
:
:s|\b(.)|$1=~tr/a-z/A-Z/|e;
:
:print $_
:
:However, perl dumps core (segmentation fault) on this. Looks like a bug to
:me (or otherwise I should get an errormessage)

Yes, you can't do that (right now).  The $N variables aren't really
references back into the string, although I've talked a bit to Larry
about it.  What you have there wouldn't work anyway, as tr/// returns
the number of substitutions.  This works:

    s/\b(.)/($x = $1) =~ tr|a-z|A-Z|, $x/ge;

although I'd really rather be able to do this:

    s/\b(.)/\u$1/g;

or more precisely:

    s/\b([a-z])/\u$1/g;

But perl doesn't support \l, \u, \L, \U, and \E.  I think it would make
things like this a lot easier, and probably more efficient as well.

--tom
--

 "All things are possible, but not all expedient."  (in life, UNIX, and perl)



Tue, 03 Aug 1993 23:31:33 GMT  
 Capitalizing words in a line.
: But perl doesn't support \l, \u, \L, \U, and \E.  I think it would make
: things like this a lot easier, and probably more efficient as well.

But it will.  And yes, they'll be more efficient.  Mostly I'm putting them
in because people will expect them to be there.

Incidentally, I think Perl will do a better job than vi: you'll be able to
say \u\L$& to force both initial cap and subsequent lowercase.

Larry



Wed, 04 Aug 1993 04:27:28 GMT  
 Capitalizing words in a line.

Quote:
>I tried doing a s|\w.|tr[a-z][A-Z]| and some variants, but nothing worked
>correctly.  I plead to the perl community for help.

This worked for me:

    s/\b([a-z])/$x=$1,$x=~tr#a-z#A-Z#,$x/ge

--

 "I want to mention that my opinions whether real or not are MY opinions."
             -- the inevitable William "Billy" Steinmetz



Wed, 04 Aug 1993 01:28:10 GMT  
 Capitalizing words in a line.

: >I tried doing a s|\w.|tr[a-z][A-Z]| and some variants, but nothing worked
: >correctly.  I plead to the perl community for help.
:
: This worked for me:
:
:     s/\b([a-z])/$x=$1,$x=~tr#a-z#A-Z#,$x/ge

In 4.0 beta, you can say

        s/\b([a-z])/\u$1/g;

which vi users may recognize.  In fact, you could get away with

        s/\b./\u$&/g;

though that does twice as much work.

Larry



Sat, 07 Aug 1993 04:24:31 GMT  
 Capitalizing words in a line.

: You know, by the time you get some with all this, the "Swiss Army CHainsaw" is
: going to be more like a Swiss Army Tactical Nuke....  :-)

Tactical?  TACTICAL!?!?  Hey, buddy, we went from kilotons to megatons
several minutes ago.  We don't need no stinkin' tactical nukes.

(By the way, do you have change for 10 million people?)

And how do you know how long it will be till I get some, anyway?

:-)

Larry



Mon, 09 Aug 1993 10:04:41 GMT  
 Capitalizing words in a line.

+---------------
| But it will.  And yes, they'll be more efficient.  Mostly I'm putting them
| in because people will expect them to be there.
|
| Incidentally, I think Perl will do a better job than vi: you'll be able to
| say \u\L$& to force both initial cap and subsequent lowercase.
+---------------

You know, by the time you get some with all this, the "Swiss Army CHainsaw" is
going to be more like a Swiss Army Tactical Nuke....  :-)

++Brandon
--
Me: Brandon S. Allbery                      VHF/UHF: KB8JRR on 220, 2m, 440

America OnLine: KB8JRR                      AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY



Mon, 09 Aug 1993 08:24:12 GMT  
 Capitalizing words in a line.
Thanks to Bill Mann and Tom Christiansen who provided solutions to "how
to capitalize words on a line."  Their nearly identical codes:

        s/\b([a-z])/$b=$1,$b=~tr:a-z:A-Z:,$b/ge;
AND
        s#(\b.)#($x = $1) =~ tr/a-z/A-Z/, $x#ge;

both operating on $_, of course. I find the second to be a bit "trickier"
to read, but both work just fine.  

        -Peter "knit one, perl two" Marvit

: Peter Marvit   Hewlett-Packard Labs in Palo Alto, CA   (415) 857-6646    :



Mon, 09 Aug 1993 04:20:37 GMT  
 Capitalizing words in a line.
Well another conumdrum, this time one of efficiency.  I created several
subroutines for different capitalization needs -- the first word only,
every word, etc.

One of the routines leaves every word in the string alone (e.i., all
UPPER CASE) but changes only first word to Mixed Case.  Unfortunately, my
first code will cause the perl script to grow in memory usage, apparently
without bound as a function of input data:

        sub capitalizefirstonly {
            # Get the string itself
            $_ = $_[1];
            # Make the whole first word lower case
            s/\b([A-Z]*)/$b=$1,$b=~tr:A-Z:a-z:,$b/e;   # <---------
            # Now capitalize the first word
            s/\b([a-z])/$b=$1,$b=~tr:a-z:A-Z:,$b/e;
            # Return the whole string
            return($_);
        }

It also chews up mucho CPU, though the memory usage is the real killer
(est. 2-4X input data size).  We tried many variations and the the key
code which causes the problems is the line which turns the first word
into lower case (marked "<-----" above). Our eventual solution, which
appears to be much more efficient time and memory-wise:

        sub capitalizefirstonly {


            $words[1] =~ tr/A-Z/a-z/;
            $words[1] =~ s/\b([a-z])/$b=$1,$b=~tr:a-z:A-Z:,$b/e;

            return($_);
        }

My question?  What's wrong with the first method.  More straight forward,
but a horrible memory pig.  Is this a perl bug (heaven forbid?).

        -Peter "knit 2, perl -1" Marvit

: Peter Marvit   Hewlett-Packard Labs in Palo Alto, CA   (415) 857-6646    :



Mon, 09 Aug 1993 08:24:54 GMT  
 Capitalizing words in a line.
: One of the routines leaves every word in the string alone (e.i., all
: UPPER CASE) but changes only first word to Mixed Case.  Unfortunately, my
: first code will cause the perl script to grow in memory usage, apparently
: without bound as a function of input data:
:
:       sub capitalizefirstonly {
:           # Get the string itself
:           $_ = $_[1];
:           # Make the whole first word lower case
:           s/\b([A-Z]*)/$b=$1,$b=~tr:A-Z:a-z:,$b/e;   # <---------
:           # Now capitalize the first word
:           s/\b([a-z])/$b=$1,$b=~tr:a-z:A-Z:,$b/e;
:           # Return the whole string
:           return($_);
:       }

: My question?  What's wrong with the first method.  More straight forward,
: but a horrible memory pig.  Is this a perl bug (heaven forbid?).

Yep.  Each tr was dropping a couple of chunks of memory when compiled.
This doesn't show up until you put it inside an eval.

As mentioned earlier, the way you'd want to do that subroutine under 4.0 is:

        sub capitalizefirstonly {

            $tmp =~ s/\b(\w+)/\L\u$1/;
            $tmp;
        }

Larry



Tue, 10 Aug 1993 04:01:32 GMT  
 Capitalizing words in a line.

Quote:
>You know, by the time you get some with all this, the "Swiss Army CHainsaw" is
>going to be more like a Swiss Army Tactical Nuke....  :-)

What's with the future tense?
--

"It's not a security hole, it's a SECURITY ABYSS." -- Christoph Splittgerber
   (with reference to the upage bug in Interactive UNIX and Everex ESIX)


Wed, 11 Aug 1993 02:33:20 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. How to Capitalize the first letter of a word an or whole word

2. Capitalize Words in a String

3. Capitalizing words?

4. how to capitalize words

5. Capitalizing words...

6. Capitalizing a word (ie tr xxxxxx to Xxxxxx) ???

7. Searching a WORD 6 DOC by line line number

8. FAQ 4.30 How do I capitalize all the words on one line?

9. FAQ 4.30 How do I capitalize all the words on one line?

10. FAQ 4.30 How do I capitalize all the words on one line?

11. FAQ 4.30 How do I capitalize all the words on one line?

12. FAQ 4.30 How do I capitalize all the words on one line?

 

 
Powered by phpBB® Forum Software