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

Re: Getting the number of times a regexp matches

by quidity (Pilgrim)
on Dec 07, 2000 at 17:59 UTC ( #45496=note: print w/ replies, xml ) Need Help??


in reply to Getting the number of times a regexp matches

Everyone so far seems to have missed this bit of evil context bashing:

$num_matches = () = $string =~ m/pattern/g;

This works because the () force the far right hand side to be evaluated in list context, the result of which is then reevaluated in scalar context to give the result. This is a nice example using side effects to good cause in perl.


Comment on Re: Getting the number of times a regexp matches
Download Code
Re: Re: Getting the number of times a regexp matches
by japhy (Canon) on Dec 07, 2000 at 18:30 UTC
(tye)Re: Getting the number of times a regexp matches
by tye (Cardinal) on Dec 07, 2000 at 22:39 UTC

    Right answer, but I don't approve of your explanation. The reason that you get a count is because a list assignment in a scalar context returns the number of elements on its right-hand side.

    There are a ton of other "operations that would return a list if used in a list context" [often sloppily (: referred to simply as "lists" ] that would return different information if used in a scalar context.

            - tye (but my friends call me "Tye")
Re: Re: Getting the number of times a regexp matches
by MeowChow (Vicar) on Dec 07, 2000 at 23:53 UTC
    This is an interesting construct, but I don't understand why Perl permits a constant, in this case, the empty list (), to be used in the LHS expression?

    For example, the following code spits out an error (Can't modify constant item in list assignment):

    (1) = (1,2,3);
    so why is the following legal:
    () = (1,2,3);
    Why is empty list not treated as a constant?

      First, because (1) contains a constant (the "1" part) and () contains no constants. So there are no constants being modified so why give an error complaining about you trying to modify no constants.

      Second, as Perl is implemented, I detect a clear preference toward not disallowing things even if the implementor can't think of a good use for that thing at the time. This makes sense for a TIMTOWTDI language.

      Third, we've just demonstrated a use for it. So it is a good thing it wasn't disallowed just because the use wasn't obvious at the time.

      I suspect that this working was at least partially an accident. The list assignment code was written and tested and it worked. I doubt anyone tested this degenerate list assignment. In fact, searching the standard Perl test suite, I find that this feature is not tested but it is used when testing another feature:

      # Should use magical autoinc only when both are strings print "not " unless 0 == (() = "0"..-1); print "ok 14\n"; for my $x ("0"..-1) { print "not "; } print "ok 15\n";
      So there! q-:

              - tye (but my friends call me "Tye")
      I suspect it has something to do with the way these things do work:
      ($a, $b, $c) = (1, 2, 3); ($a, $b) = (1, 2, 3); # 3 discarded ($a, undef, $c) = (1, 2, 3); # 2 discarded ($a, undef) = (1, 2, 3); # 2 and 3 discarded (undef, undef, undef) = (1, 2, 3); # 1 2 and 3 discarded () = (1, 2, 3); # functionally equivalent
      "undef" is the only real non-variable value you can use on the left-hand-side like that.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2014-04-21 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls