Re: RFC 110 (v3) counting matches

[email protected] (Joe McMahon)
Newsgroups perl.perl6.language.regex
Message-ID <[email protected]>
Philip Newton wrote:
> 
> What I use in a script of mine is:
> 
>     while ($string =~ /(\d\d)-(\d\d)-(\d\d)/g) {
>         ($mo, $dy, $yr) = ($1, $2, $3);
>     }
> 
> Although this, of course, also requires that you know the number of
> backreferences. Nicer would be to be able to assign from @matchdata or
> something like that :)
> 
I mentioned in another thread the concept (stolen from SNOBOL) of a 
conditional function call, post-match. Winging a syntax which I am 
absolutely NOT committed to:

   my @list;
   my $capture = sub {push @list, makedate(\1,\2,\3);};
   $string =~ /(\d\d)-(\d\d)-(\d\d)?&$capture/g;

In a regular, non-global match, the anonymous sub gets called if the 
match preceeding it succeeds. If it fails, the function doesn't get 
called.  In a "/g", it gets called *each time* a global match succeeds; 
you get an implicit "while" over the string.

This is NOT completely thought out yet; I've been mulling it over as a 
possible RFC. I'm not happy with this syntax as it stands - there's too 
much possible action at a distance. I'm not seeing a nicely-parseable, 
easily-understandable way of doing this. Would this be a possible:

   $string =~ /(\d\d)-(\d\d)-(\d\d)?&{push @list,makedate(\1,\2,\3)}/g;

Or is that just too ugly and nasty for words?

  --- Joe M.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.