Path: utzoo!utgpu!news-server.csri.toronto.edu!torsqnt!hybrid!scifi!bywater!uunet!convex!news From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: Can this be done more slickly Message-ID: <1991Mar16.033051.2482@convex.com> Date: 16 Mar 91 03:30:51 GMT References: Sender: news@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Distribution: comp Organization: CONVEX Software Development, Richardson, TX Lines: 43 Nntp-Posting-Host: pixel.convex.com From the keyboard of victor@ibm.com: :I wrote the function given below to transform a string with -'s in it :so that all alphabetics were lower case, except at the beginning of :the string or immediately after a dash, in which case it would be :upper case. The follow works correctly, but I was wondering (in the :JAPH spirit), if there is a more clever (or concise) way of doing it. :sub normalize { : local(@pieces) = split(/-/,$_[0]); : local($i); : foreach $i (0..$#pieces) { : $pieces[$i] =~ tr/A-Z/a-z/; : substr($pieces[$i],0,1) =~ tr/a-z/A-Z/; : } : join('-',@pieces); : } Before 4.0, you have to use this: sub normalize { local($_) = @_; tr/A-Z/a-z/; substr($_,$[,1) =~ tr/a-z/A-Z/; s/(-[a-z])/$x = tr#a-z#A-Z#, $x/ge; $_; } But at 4.0beta or above, this works, and runs in just 40% the time of my previous example: sub normalize { local($_) = @_; tr/A-Z/a-z/; substr($_,$[,1) =~ tr/a-z/A-Z/; s/-([a-z])/-\u$1/g; $_; } The first two tr's could have been done with \L and \u escapes, but the tr's run a good faster that they do in those cases. --tom