in reply to coding style suggestion: (...)[1] vs. [...]->[1]

use strict; my $string = "zero one two three"; my $offset = 1; my $regex = '[^\\s]+\\s+' x $offset . '([^\\s]+)'; print $string =~ /$regex/; __END__ one
I don't have time to test if this is faster but it will not create the intermediate array. It might be more memory efficient if $string is really huge. But it won't let you take a slice.

update now that I think about it it might not be very efficient if both $string and $offset are huge because $regex will also be huge. Just TIMTOWTDI.

update 'doh! liz's way is even better.

--

flounder

Replies are listed 'Best First'.
Re: Re: coding style suggestion
by liz (Monsignor) on Sep 19, 2003 at 12:18 UTC
    Ah, but you don't need to bother with $regex:

    my $string = "zero one two three"; my $offset = 1; print $string =~ /(?:[^\s]+\s+){$offset}([^\s]+)/; __END__ one

    And in this case the regex is not large, but may take some time to execute.

    Liz