On Mar 17, 2011, at 5:21 PM, Rob Dixon wrote:
> A s///g is 'successful' if it performs at least one substitution, in which
> case it will return the number of substitutions made. In your code, it will
> find as many key=value substrings as possible and replace them with just the
> value string.
>
> The \G pattern is documented only in the case of m//g, which makes sense as
> it is defined in terms of a character position (pos) within the string where
> the last match ended. If a substitution is being made then it will also
> affect character positions, and so is similar to adding to or deleting from
> an array while iterating over it.
>
> It is bad form to use the /e modifier to generate side-effects (just as it is
> wrong to do so with the map operator).
All very useful info - thanks.
> I believe a while loop is the proper way to go, but if you want to experiment
> with m//g I suggest something like this
>
> my %matches = $line =~ /\G\s*(\w+)(?:\s*=\s*(\w+))?\s*/gc;
>
> which will pass all the 'key' and 'key=value' pairs to %matches. An invalid
> input will cause the match to terminate before the end of the string, so
>
> if (pos $line < length $line) {
> # code to handle bad input
> }
>
> If a key has no corresponding value in the string it will appear in the hash
> with a value of undef, which should be defaulted to 1 like this
>
> foreach (values %matches) {
> $_ = 1 if not defined;
> }
>
> I hope that helps a little.
It helped a lot. At this point I'd agree that the while loop is the most
straightforward approach.
Thanks again.
Chap
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/