On Friday, September 5, 2003, at 08:52 AM, Gary Stainburn wrote:

On Friday 05 Sep 2003 2:25 pm, Jenda Krynicky wrote:
From: Gary Stainburn <[EMAIL PROTECTED]>

I've got to tidy some data, including converting case.  I need to
convert

ANOTHER COMPANY NAME LTD **

to

Another Company Name Ltd **

while retaining spaces.

$text =~ s/(\w+)/\L\u$1/g;$y

Thanks Jenda,


BTW, I did get the split/join verysion working - don't know what I'd done
wrong.


Which method would be quickest?

Running:


#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

my($text1, $text2) = ('ANOTHER COMPANY NAME LTD') x 2;

timethese( 1000000, { Search_and_Replace => '$text1 =~ s/(\w+)/\L\u$1/g;',
Split_and_Join => '$text2 = join \'\', map { ucfirst(lc $_) } split /(\s+)/, $text2;' } );


__END__

I get:

Benchmark: timing 1000000 iterations of Search_and_Replace, Split_and_Join...
Search_and_Replace: 0 wallclock secs ( 0.93 usr + 0.00 sys = 0.93 CPU) @ 1075268.82/s (n=1000000)
Split_and_Join: 2 wallclock secs ( 2.22 usr + 0.00 sys = 2.22 CPU) @ 450450.45/s (n=1000000)


So, the search and replace method (Jenda's solution) appears to be much faster.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to