From:                   Jeff 'japhy' Pinyan <[EMAIL PROTECTED]>
> On Feb 14, Bill Akins said:
> 
> >I have a string that is read in and assigned to a veriable.  String
> >looks something like this: 10.00 c$cpi  c$ul  (Sample Number:) c$sh 
> >/Courier 0 c$fnt  ( SA-01-0C8A8) c$sh ( ) c$sh  /Courier 0 c$fnt
> >
> >I need the string between the second set of ()'s.  There may or may
> >not be a leading space, if so, I need to strip it out.  There are not
> >the exact same number of charecters in the string, the only thing
> >constant is the value I need is in the second set of ()'s.
> 
> You can match it like so:
> 
>   my ($second_paren) = ($string =~ /\((.*?)\)/g)[1];
> 
> or
> 
>   my ($second_paren) = ($string =~ /\(([^)]*)\)/g)[1];

#!perl
use Benchmark;

$string = '10.00 c$cpi  c$ul  (Sample Number:) c$sh  /Courier 0 c$fnt  (SA-01-0C8A8) 
c$sh ( ) c$sh  /Courier 0 c$fnt';

sub Japhy1 {
        my $second_paren;
        for(my $i=0;$i<1000;$i++) {
                $second_paren = ($string =~ /\((.*?)\)/g)[1];
        }
}

sub Japhy2 {
        my $second_paren;
        for(my $i=0;$i<1000;$i++) {
                $second_paren = ($string =~ /\(([^)]*)\)/g)[1];
        }
}

sub Mine1 {
        my $second_paren;
        for(my $i=0;$i<1000;$i++) {
                $second_paren = ($string =~ /^.*?\(.*?\).*?\((.*?)\)/);
        }
}

sub Mine2 {
        my $second_paren;
        for(my $i=0;$i<1000;$i++) {
                $second_paren = ($string =~ /^[^)]*\([^)]*\)[^)]*\(([^)]*)\)/);
        }
}

timethese 1000, {
        Japhy1 => \&Japhy1,
        Japhy2 => \&Japhy2,
        Mine1 => \&Mine1,
        Mine2 => \&Mine2,
}
__END__

Benchmark: timing 1000 iterations of Japhy1, Japhy2, Mine1, Mine2...
    Japhy1: 14 wallclock secs (13.76 usr +  0.00 sys = 13.76 CPU) @ 72.68/s (n=1000)
    Japhy2:  9 wallclock secs ( 8.95 usr +  0.00 sys =  8.95 CPU) @ 111.69/s (n=1000)
     Mine1:  7 wallclock secs ( 6.25 usr +  0.00 sys =  6.25 CPU) @ 160.03/s (n=1000)
     Mine2:  5 wallclock secs ( 5.03 usr +  0.00 sys =  5.03 CPU) @ 198.89/s (n=1000)

So it's better to skip explicitly the first () pair then to match all () groups and 
then take 
the second one. And /\([^)]*\)/ is quicker than /\(.*?\)/ (The two DO NOT mean exactly 
the same!!!)

Jenda

=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
                                        --- me

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

Reply via email to