in reply to An optimization of last resort: eliminate capturing from your regexps
my $word = substr $str, $-[0] + 10, $+[0] - $-[0] + 10;
There seems to be a typo there:
my $str = 'Hulk hate classless system with means of manufacturing give +n to working class! Hulk crush puny Marxists!'; if ($str =~ /Hulk hate \w+/) { print "\$-[0] <$-[0]> \$+[0] <$+[0]>\n"; my $word = substr $str, $-[0] + 10, $+[0] - $-[0] + 10; print "word <$word>\n"; } __END__ $-[0] <0> $+[0] <19> word <classless system with means o>
That +10 should be -10.
Anyway, the reason I'm replying is not this, but to suggest some typeglob aliasing to make the code a tiny bit easier to read:
*starts = \@-; *ends = \@+; our (@starts, @ends); my $str = 'Hulk hate classless system with means of manufacturing give +n to working class! Hulk crush puny Marxists!'; if ($str =~ /with means of \w+/) { print "\$-[0] <$-[0]> \$+[0] <$+[0]>\n"; my $word = substr $str, $starts[0] + 14, $ends[0] - $starts[0] - 1 +4; print "word <$word>\n"; } __END__ $-[0] <27> $+[0] <54> word <manufacturing>
--
David Serrano
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: An optimization of last resort: eliminate capturing from your regexps
by diotalevi (Canon) on Jul 11, 2006 at 13:53 UTC |