robmueller has asked for the wisdom of the Perl Monks concerning the following question:
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: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 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+) ?/gc)) { 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?) Robwhile (... not finished string ...) { if (my @m = ($a =~ /\G(?:(\w+) ?)+/c)) { push @s, @m; } elsif ... other stuff not important }
|
|---|
| 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 | |
|
Re: m/\G..blah../gc matching in list context
by merlyn (Sage) on Jan 02, 2002 at 20:01 UTC |