Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: An optimization of last resort: eliminate capturing from your regexps

by Hue-Bond (Priest)
on Jul 11, 2006 at 08:22 UTC ( [id://560354]=note: print w/replies, xml ) Need Help??


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

    Hey, that's nicer to look at. Good names are always a plus. I also updated my original post to include your bugfix. I left it there but put a note next to it going "Hey! See! You could screw up like this too if you do this."

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://560354]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found