Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Named Captures

by BrowserUk (Pope)
on Oct 06, 2010 at 11:15 UTC ( #863758=note: print w/replies, xml ) Need Help??


in reply to Re: Named Captures
in thread Named Captures

Okay. What is going on here then?

perl -wE"'abcde' =~ m[(?<a>a)|(?<b>b)|(?<c>c)|(?<d>d)|(?<e>e)]g and do +{ say for keys %+ }" a perl -wE"()='abcde' =~ m[(?<a>a)|(?<b>b)|(?<c>c)|(?<d>d)|(?<e>e)]g and + do{ say for keys %+ }" e perl -wE"my@a = 'abcde' =~ m[(?<a>a)|(?<b>b)|(?<c>c)|(?<d>d)|(?<e>e)]g + and do{ say for keys %+ }" e perl -wE"my@a = 'abcde' =~ m[(?<a>a)|(?<b>b)|(?<c>c)]g and do{ say for + keys %+ }" ## big fat nothing?

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: Named Captures
by Corion (Pope) on Oct 06, 2010 at 11:20 UTC
    perl -wE"'abcde' =~ m[(?<a>a)|(?<b>b)|(?<c>c)|(?<d>d)|(?<e>e)]g and do +{ say for keys %+ }" a

    /g in scalar context matches once, %+ contains the first value, a.

    perl -wE"()='abcde' =~ m[(?<a>a)|(?<b>b)|(?<c>c)|(?<d>d)|(?<e>e)]g and + do{ say for keys %+ }" e

    /g in list context matches all locations, and after having matched all, %+ contains the result of the last match, e.

    perl -wE"my@a = 'abcde' =~ m[(?<a>a)|(?<b>b)|(?<c>c)|(?<d>d)|(?<e>e)]g + and do{ say for keys %+ }" e

    Same as above, except that you store the intermediate values.

    perl -wE"my@a = 'abcde' =~ m[(?<a>a)|(?<b>b)|(?<c>c)]g and do{ say for + keys %+ }" ## big fat nothing?

    You have removed [de] from the matching class. %+ is a global variable and it (seems it) is cleared by the last failing match.

    It might or might not be nice to have %+ retain the value(s) of the last successful match, but maybe you can work around this by padding your RE with .*$re.* to convert it into a truely floating RE.

      Okay. That begins to make sense ;)

      But, I discovered that I can get (sort of) what I was after by switching to %-

      perl -wE"()='abcde' =~ m[(?<a>a)|(?<b>b)|(?<c>c)|(?<d>d)|(?<e>e)]g and + do{ say for keys %- }" e c a b d

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Not quite.
        () = 'abc' =~ /(?<a>a)|(?<f>f)/ and do {say for keys %-}; __END__ a f
        %- will have a key for every named capture in the pattern, whether that capture was involved in the match or not.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863758]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2016-12-08 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (148 votes). Check out past polls.