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

Re^2: Behavior of /g when there are capture groups

by AnomalousMonk (Archbishop)
on Apr 21, 2016 at 19:18 UTC ( [id://1161145]=note: print w/replies, xml ) Need Help??


in reply to Re: Behavior of /g when there are capture groups
in thread Behavior of /g when there are capture groups

This could be generalized to extract substrings, not just substring initial offsets; also to avoid  $& which just slows everything down. (The many evals might slow things down, though. Oh, well...) (Also tested under 5.8.9.)

c:\@Work\Perl>perl -wMstrict -le "use Data::Dumper; ;; $_ = 'abcdfoofrobnicatebardefforspambazghi'; ;; my $re = qr{ (fo.) (.*?) (ba.) }xms; ;; my %res; while (/($re)/g) { $res{$1} = [ map eval qq{\$$_}, 2 .. $#- ]; } print Dumper \%res; " $VAR1 = { 'forspambaz' => [ 'for', 'spam', 'baz' ], 'foofrobnicatebar' => [ 'foo', 'frobnicate', 'bar' ] };


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Behavior of /g when there are capture groups
by Eily (Monsignor) on Apr 21, 2016 at 22:18 UTC

    I should have tested my code, I got confused and thought @- held the submatches themselves, not the indexes. So I actually meant $res{$&} = [ $1, $2, $3]; but got lazy :). And $res{$1} = [ $2, $3, $4 ] works without $& with the extra parentheses.

    Your proposition has the benefit of not requiring to know the inner regex and its number of captures :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found