develooper Front page | perl.perl5.porters | Postings from November 1999

Re: [ID 19991130.003] Assignment to List Breaks \G.

Thread Previous | Thread Next
From:
Ronald J Kimball
Date:
November 30, 1999 08:32
Subject:
Re: [ID 19991130.003] Assignment to List Breaks \G.
Message ID:
19991130113234.B683545@linguist.dartmouth.edu
On Tue, Nov 30, 1999 at 04:10:20PM +0000, Ralph Corderoy wrote:
> 
> Hi,
> 
> Assigning a /gx regexp to a list breaks \G in the following regexp.
> 
>     #! /usr/local/bin/perl -w
> 
>     $_ = 'a 1 b 2 c 3';
> 
>     print "bug\n";
>     ($a, $b) = /^(\w)\s(\d)\s/gx;
>     print "a=$a b=$b\n";
>     ($a, $b) = /\G(\w)\s(\d)/gx;
>     print "a=$a b=$b\n";
> 
>     print "workaround\n";
>     /^(\w)\s(\d)\s/gx;
>     ($a, $b) = ($1, $2);
>     print "a=$a b=$b\n";
>     ($a, $b) = /\G(\w)\s(\d)/gx;
>     print "a=$a b=$b\n";
> 
> Output is
> 
>     bug
>     a=a b=1
>     a=a b=1
>     workaround
>     a=a b=1
>     a=b b=2
> 

This is the expected behavior for m//g in a list context.  The regular
expression is applied repeatedly until it no longer matches, and the return
value is a list of all the substrings matched.  After the final application
of the regex, which fails to match, pos() is reset to the beginning of the
string.


Try this instead:

    #! /usr/local/bin/perl -w

    $_ = 'a 1 b 2 c 3';

    @a = /\G(\w)\s(\d)\s?/gx;
    $" = ',';
    print "@a\n";

Output:

    a,1,b,2,c,3

Ronald

Thread Previous | Thread Next


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