Mr. Shawn H. Corey wrote:
On Tue, 2008-11-04 at 16:30 +0100, Rob Coops wrote:
What you want to be doing is this: $temp =~ s/\s+/ /g;
Actually to substitute multiple spaces with just one:
$temp =~ s/ +/ /g;
Or as some prefer:
$temp =~ s{ [ ]+ }{ }gx;
Not if speed is an issue:
$ perl -le'
use Benchmark q/cmpthese/;
my $temp = join "X", map { " " x ( 1 + rand 10 ) } 1 .. 10_000;
cmpthese -10, {
trans => sub { my $x = $temp; $x =~ tr/ //s; return $x },
subcc => sub { my $x = $temp; $x =~ s/ [ ]+ / /xg; return $x },
sublit => sub { my $x = $temp; $x =~ s/ \ + / /xg; return $x },
}
'
Rate subcc sublit trans
subcc 235/s -- -4% -85%
sublit 245/s 4% -- -85%
trans 1582/s 574% 546% --
And in fact, as of version 5.10, Perl will internally convert a
character class with a single character to a character literal.
Or even:
$temp =~ s/\x20+/\x20/g;
The reason for not using the first is that sometimes whitespace is
ignored (like you normally do in Perl), making it harder to understand.
Whitespace is usually ignored in Perl code, not in data used in Perl
code. The /x option was added to regular expressions to allow them to
be formatted to look more like code.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/