On Fri, May 28, 2010 at 12:44, Uri Guttman <u...@stemsystems.com> wrote: >>>>>> "CO" == Chas Owens <chas.ow...@gmail.com> writes: > > CO> On Fri, May 28, 2010 at 01:05, Uri Guttman <u...@stemsystems.com> wrote: > CO> snip > >> $foo = $string =~ /^([^-])+-/ ? $1 : '' ; > >> > >> that will grab something from the start to the first - and grab it. if > >> it matched it will assign it to $foo, otherwise assign ''. (and '' is > >> called the null string, not null. perl has no null things unlike > >> databases). > CO> snip > > CO> This seems like an overcomplicated regex to me, what is the benefit of > CO> doing this over > > CO> my ($foo) = $string =~ /(.*?)-/; # $foo will be undef if there is > CO> no match > > the negated char class is usually faster than most similar methods. i > just like it as it says what i really want - a string without any - chars. > also anchoring helps too in saying this string must be at the beginning > (he wants the first field).
Really? I have never found it so. Could you explain what is wrong with my benchmark (I get similar results on 5.12)? Perl version 5.008008 anchored_nongreedy => foo charclass => foo nongreedy => foo foo: Rate charclass anchored_nongreedy nongreedy charclass 748127/s -- -5% -7% anchored_nongreedy 783752/s 5% -- -3% nongreedy 805306/s 8% 3% -- foo-: Rate charclass anchored_nongreedy nongreedy charclass 750868/s -- -3% -10% anchored_nongreedy 778073/s 4% -- -6% nongreedy 829906/s 11% 7% -- xxxx: Rate charclass anchored_nongreedy nongreedy charclass 753628/s -- -4% -8% anchored_nongreedy 783752/s 4% -- -5% nongreedy 822940/s 9% 5% -- xxx-: Rate charclass anchored_nongreedy nongreedy charclass 759211/s -- -2% -7% anchored_nongreedy 778073/s 2% -- -4% nongreedy 813440/s 7% 5% -- #!/usr/bin/perl use strict; use warnings; use Benchmark; print "Perl version $]\n"; my $s = "foo-"; my %subs = ( nongreedy => sub { my ($x) = $s =~ /(.*?)-/; return $x; }, anchored_nongreedy => sub { my ($x) = $s =~ /^(.+?)-/; return $x; }, charclass => sub { my ($x) = $s =~ /^([^-]+)-/; return $x; }, ); for my $k (keys %subs) { print "$k => ", $subs{$k}(), "\n"; } for $s ( "foo", "foo-", "x" x 10_000, ("x" x 10_000) . "-") { print "\n", substr($s, -4, 4), ":\n"; Benchmark::cmpthese -1, \%subs; } -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/