Hi Aureliano, It's a good question. The short answer is I haven't had
any memory problems with the toy examples so far, but I haven't scaled
up the regex to know how it behaves when testing for hundreds (or
thousands) of matches. I suppose there might be some way to restrict
array values to Int-only; that would save some space.
Either way, I think it's pretty exciting that I can add extra lines to
the regex--lines that are identical except for the search string--and
it works (example below). This seems like a huge improvement in
"regex-ology".
Please copy the list since we all benefit from these communications.
Myself, I'm interested in finding out how $/.pos knows not to fill the
@a array with duplicate values on failed matches. It works for now,
and (if it wasn't clear from my last email), the regex can be
rearranged to return a final "success" value if found at the proper
position (the end) of an input string:
use v6
my $test = " foo bar success";
my @a = 0;
say $test ~~ {
m /^\s+ {@a.push($/.pos)}/;
m :pos(@a[*-1]) /foo\s+ {@a.push($/.pos)}/;
m :pos(@a[*-1]) /willnotmatch\s+ {@a.push($/.pos)}/;
m :pos(@a[*-1]) /bar\s+ {@a.push($/.pos)}/;
m :pos(@a[*-1]) /success {@a.push($/.pos)}/;
}
#returns 「success」
HTH, Bill.
On Mon, Aug 19, 2019 at 5:02 PM Aureliano Guedes
<[email protected]> wrote:
>
> Commonly, in my experience, this kind of operation has no more a memory
> problem (nowadays we most account with some Gb of memory, sometimes Tb) but
> storing in an array or hash may lead us to unnecessary memory usage, right?
> Sorry for the newbie silly question.
>
> On Mon, Aug 19, 2019 at 8:13 PM William Michels <[email protected]> wrote:
>>
>> Thanks to Brad Gilbert's code contribution in this thread, I re-wrote
>> a small snippet of his code (code that incrementally checks a series
>> of regex matches), to have it return the last position of each match.
>> Testing with three 'matches' and one 'willnotmatch' returns three
>> positional values, as expected:
>>
>> use v6
>> my $test = " foo bar";
>>
>> sub foo($x) {
>> state @a = 0;
>> $x ~~ m /^\s+ {@a.push($/.pos)}/;
>> $x ~~ m :pos(@a[*-1]) /foo\s+ {@a.push($/.pos)}/;
>> $x ~~ m :pos(@a[*-1]) /willnotmatch {@a.push($/.pos)}/;
>> $x ~~ m :pos(@a[*-1]) /bar {@a.push($/.pos)}/;
>> return @a[1 .. *];
>> }
>>
>> #say foo($test); # returns (6 10 13)
>> put foo($test); # returns 6 10 13
>>
>>
>> I'm actually pleasantly surprised that I can add a dozen or so
>> 'willnotmatch' lines, and it doesn't screw up the result. The next
>> step might be to 1). pull the individual regexes out into an object
>> (as suggested in the SO post below) to simplify each smartmatch line,
>> and/or 2). store the results in a hash (instead of an array), for
>> later substring extraction. But at this point it seems I'm getting
>> into 'Grammar' territory, so that might be the better approach.
>>
>> HTH, Bill.
>>
>> https://stackoverflow.com/questions/50829126/perl6-interpolate-array-in-match-for-and-or-not-functions/50838441#50838441
>>
>>
>>
>>
>>
>>
>>
>>
>> On Mon, Aug 19, 2019 at 1:08 AM Patrick Spek via perl6-users
>> <[email protected]> wrote:
>> >
>> > On Sun, 18 Aug 2019 13:45:27 -0300
>> > Aureliano Guedes <[email protected]> wrote:
>> >
>> > > Even being another language, Perl6 should be inheriting Perl5's
>> > > regexes or even improving it not making it uglier and harder.
>> > >
>> > > Or I'm seeing how to use it in an easy way. Also, dunno if there is
>> > > some GOOD documentation to Perl6 regexes.
>> >
>> > Beauty is in the eye of the beholder. While I'm much more proficient
>> > with PCRE than P6CRE, I do find the Perl 6 variants to be much cleaner
>> > and easier to understand when reading regexes of others.
>> >
>> > If you find that there's a lack of documentation explaining things
>> > clearly to you, that'd be an issue to solve in the documentation. This
>> > takes a lot of effort, and if you would be so kind as to improve it
>> > where you think it's needed, it would be a great help to everyone (we
>> > can't really see how or where you're looking for what, after all).
>> >
>> > --
>> > With kind regards,
>> >
>> > Patrick Spek
>> >
>> >
>> > www: https://www.tyil.nl/
>> > mail: [email protected]
>> > pgp: 1660 F6A2 DFA7 5347 322A 4DC0 7A6A C285 E2D9 8827
>> >
>> > social: https://soc.fglt.nl/tyil
>> > git: https://gitlab.com/tyil/
>
>
>
> --
> Aureliano Guedes
> skype: aureliano.guedes
> contato: (11) 94292-6110
> whatsapp +5511942926110