robmueller has asked for the wisdom of the Perl Monks concerning the following question:

I've had a look around, but can't find whether this is a known issue or I'm doing something stupid. Basically I'm trying to extract a series of tokens from a string. Classic use of the m/\G.../gc construct. I have a loop as such:
my $a = 'first second ...other non-\w char groups...'; my @s; while (... not finished string ...) { if ($a =~ /\G(\w+) ?/gc) { push @s, $1; } elsif ... other stuff not important }
Now most of the string happens to be \w+ type tokens, so I thought it might be a useful performance optimisation to try and grab a whole heap of matches at once as such:
while (... not finished string ...) { if (my @m = ($a =~ /\G(\w+) ?/gc)) { push @s, @m; } elsif ... other stuff not important }
Now the matches work correctly, but it doesn't update the internal pos(),\G marker, so the next match occurs at the same place again (eg infinite loop). I also tried something like:
while (... not finished string ...) { if (my @m = ($a =~ /\G(?:(\w+) ?)+/c)) { push @s, @m; } elsif ... other stuff not important }
But it only returns the last match and doesn't appear to update pos(),\G either. So a couple of questions. Is there a way to grab multiple matches with m/\G.../gc? Am I dreaming that any of this might be a performance win? (what about long strings where $' has already been referenced so there's lots of string copying to do after each match?) Rob

Replies are listed 'Best First'.
Re: m/\G..blah../gc matching in list context
by Zaxo (Archbishop) on Jan 02, 2002 at 09:23 UTC

    Do one of these do what you want?

    $_ = 'first second ...other non-\w char groups...'; my @s; @s = /\w+/g; # or else @s = split " ";
    If not, please describe your data in more detail and indicate what parts of it you want to pick.

    After Compline,
    Zaxo

Re: m/\G..blah../gc matching in list context
by merlyn (Sage) on Jan 02, 2002 at 20:01 UTC