Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Named Captures

by BrowserUk (Pope)
on Oct 06, 2010 at 10:58 UTC ( #863754=perlquestion: print w/ replies, xml ) Need Help??
BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Why does this only capture once?

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

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.

Comment on Named Captures
Download Code
Re: Named Captures
by JavaFan (Canon) on Oct 06, 2010 at 11:03 UTC
    Because you performed m//g in scalar context.

    Note that if you had done the match in list context, again only one key in %+ would be set ("d" in that case). After all, you wouldn't expect $16 to be set either, would you?

      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.
        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.

Re: Named Captures
by Corion (Pope) on Oct 06, 2010 at 11:09 UTC

    You don't have a loop around the /g match, so it only matches once and uses the first matching alternative, "a".

    The following loops over the string and outputs %+ whenever it matches something:

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

    I don't think that %+ will ever be filled with keys from more than one part of an alternation, so you'll never find more than one key in your example. To get all keys you'll likely need to introduce an explicit ordering or enumerate the alternatives.

      So, each iteration of the implicit /g loop will overwrite %+. Ie. they don't accumulate?


      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://863754]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-09-03 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls