Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!sdd.hp.com!hplabs!marvit From: marvit@hplpm.hpl.hp.com (Peter Marvit) Newsgroups: comp.lang.perl Subject: Re: Capitalizing words in a line. Message-ID: Date: 21 Feb 91 00:24:54 GMT References: Sender: news@hplabs.hpl.hp.com (placeholder for future) Organization: Hewlett-Packard Laboratories, Palo Alto, CA Lines: 45 In-Reply-To: marvit@hplpm.hpl.hp.com's message of 20 Feb 91 15:20:37 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 { local($b,@words); @words = split(' ',$_[1]); $words[1] =~ tr/A-Z/a-z/; $words[1] =~ s/\b([a-z])/$b=$1,$b=~tr:a-z:A-Z:,$b/e; $_ = join(' ',@words); 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 : : Internet: uucp: {any backbone}!hplabs!marvit :