Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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.

Replies are listed 'Best First'.
(tye)Re: Getting the number of times a regexp matches
by tye (Sage) 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 japhy (Canon) on Dec 07, 2000 at 18:30 UTC
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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2021-10-17 20:31 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (72 votes). Check out past polls.