develooper Front page | perl.perl5.porters | Postings from September 2010

[perl #41011] "backslash-G" does not appear to work in a while loop

From:
Father Chrysostomos via RT
Date:
September 26, 2010 13:54
Subject:
[perl #41011] "backslash-G" does not appear to work in a while loop
Message ID:
rt-3.6.HEAD-5116-1285534442-951.41011-15-0@perl.org
On Tue Nov 28 14:04:29 2006, James.Woodworth@ricardo.com wrote:
> In a "while" loop, the "backslash-G" does not seem to work for
> capturing data of multiple key/value pairs from a common line.
> 
> It was expected that each "<datakey>=<spaces><datavalue>"
> would have been captured and printed in this trivial example.
> 
> ################################################################
> # sample program that shows only the first key value pair
> # is captured for use.
> 
> # contact info: Jim Woodworth -- james.woodworth@ricardo.com
> # thanks.
> 
> use warnings;
> use strict;
> 
> while( defined( my $line = <DATA> ))
> {
>     chomp $line;
>     while( my( $k, $v ) = $line =~ /\G\s*(\S+)=\s+(\S+)/gc )
>     {
>         print "$line\n====k: $k -- v: $v\n";
>     }
> }

When //g is called in list context, it keeps match again and again till
the end of the string. Then it returns all the captured text as a list.

In scalar context it matches once and remembers its position.

You are calling it in list context.




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About