in reply to Re: regex needed
in thread regex needed

it will split into 3 parts

I'm not sure there's any guarantee perl will do that optimization for you. If you want three parts, just tell it so:

$foo = (split/:/,$_,3)[1];
Update: You seem to be right about the optimization. The following benchmark shows nearly identical times:
cchan.acsys.com.1.256% cat aaa3.pl use Benchmark; my $count = 1000000; our $str = "a:b:c:d:e:ff:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z"; timethese($count, { 'slice' => sub { (undef, my $foo) = split /:/,$str; }, 'count' => sub { my $foo = (split/:/,$str,3)[1]; }, });
Benchmark: timing 1000000 iterations of count, slice... count: 2 wallclock secs ( 3.18 usr + 0.01 sys = 3.19 CPU) @ 31 +3479.62/s (n=1000000) slice: 3 wallclock secs ( 3.04 usr + 0.06 sys = 3.10 CPU) @ 32 +2580.65/s (n=1000000)
However, giving the split limit explicitly is clearer IMHO.

Replies are listed 'Best First'.
Re: Re: Re: regex needed
by bart (Canon) on Mar 14, 2003 at 20:15 UTC
    I'm not sure there's any guarantee perl will do that optimization for you.
    Yes it's garanteed. I'll just have to find the proper documentation for you... Ah, here it is, in perldoc -f split:
    When assigning to a list, if LIMIT is omitted, Perl supplies a LIMIT one larger than the number of variables in the list, to avoid unnecessary work.
    Therefore, with two scalars on the left hand side (of which one variable, and undef), Perl will split into 3 parts.