A question about m/PATTERN/g - first match disappears

A

Allan M. Due

Hi Folks,

Can someone clear up my misunderstanding of the match operator. I don't
understand why the following occurs. First script:

-----
use strict;

my $string = 'one1 blather one2 blather blather one3 One4';
my @array;

if ($string =~ m/one\d+/gis) {
(@array) = $string =~ m/one\d+/gis;
}

print "@array";
------

produces:

one2 one3 One4

I expected the one1 to be there but it isn't

If change the if line so there is no g

-------
use strict;

my $string = 'one1 blather one2 blather blather one3 One4';
my @array;

if ($string =~ m/one\d+/is) {
(@array) = $string =~ m/one\d+/gis;
}

print "@array";
--------

I get:

one1 one2 one3 One4

Why does the first not capture one1 into @array?

I've read the docs but the reason completely eludes me.

TIA

AmD
 
A

A. Sinan Unur

Can someone clear up my misunderstanding of the match operator. I
don't understand why the following occurs. First script:

-----
use strict;

my $string = 'one1 blather one2 blather blather one3 One4';
my @array;

if ($string =~ m/one\d+/gis) {
(@array) = $string =~ m/one\d+/gis;
}

Now, why do you want to do that? :) You may have overlooked this, but I
think the most succint explanation is given by the documentation for
pos. When you match a string using a regex with the g modifier, the
position of the match is stored, so that the next match can match the
next occurence (if any). Otherwise, it would not be possible to do:

#!/usr/bin/perl

use strict;
use warnings;

my $s = q{one1 blather one2 blather blather one3 One4};

while ( $s =~ m{ (one\d) }gimsx ) {
print "$1\n";
}

__END__
if ($string =~ m/one\d+/is) {
(@array) = $string =~ m/one\d+/gis;
}

There is no need to match twice:

if ( @matches = ($s =~ m{ (one\d) }gimsx) ) {
print "@matches\n";
}

I don't know what that is but your sig separator is not correct: It
should be dash-dash-space-newline.

Sinan
 
A

Allan M. Due

A. Sinan Unur wrote:

<snip excellent clarification and code suggestion>

Thanks for clearling that up for me, much appreciated.
I don't know what that is but your sig separator is not correct: It
should be dash-dash-space-newline.


My sig separator is dash-dash-space-newline in my sig file. Must have
messed it up when I posted. Sorry about that.

A Sig P228 is just that a Sig P228. Give it a Google if you are
interested.
 
A

A. Sinan Unur

A. Sinan Unur wrote:

<snip excellent clarification and code suggestion>

Thanks for clearling that up for me, much appreciated.

You are welcome.
My sig separator is dash-dash-space-newline in my sig file. Must have
messed it up when I posted. Sorry about that.

No problem. My newsreader automatically snips properly formatted
signatures, that's why I noticed it.
A Sig P228 is just that a Sig P228. Give it a Google if you are
interested.

I see. Thanks.

Sinan
 
A

A. Sinan Unur

A. Sinan Unur ([email protected]) wrote on MMMMCDLVI September
MCMXCIII in <URL:~~
~~ Now, why do you want to do that? :) You may have overlooked this,
~~ but I think the most succint explanation is given by the
~~ documentation for pos. When you match a string using a regex with
~~ the g modifier, the position of the match is stored, so that
~~ the next match can match the next occurence (if any).

That's for a /g match *IN SCALAR CONTEXT*. In list context, it's quite
different.

The OP had a /g match in scalar context, followed by a /g match in list
context on the same string. My assumption is that the /g match in list
context started from where the match in scalar context left off. The
explanation for the behavior arose from the way /g behaves in scalar
context. Am I missing something?

Sinan
 
A

A. Sinan Unur

A. Sinan Unur ([email protected]) wrote on MMMMCDLVI September
MCMXCIII in <URL:&& &&
&& > A. Sinan Unur ([email protected]) wrote on MMMMCDLVI
&& > September MCMXCIII in <URL:&& > 127.0.0.1:
&& > ~~
&& > ~~ Now, why do you want to do that? :) You may have overlooked
&& > ~~ this, but I think the most succint explanation is given by
&& > ~~ the documentation for pos. When you match a string using a
&& > ~~ regex with the g modifier, the position of the match is
&& > ~~ stored, so that the next match can match the next occurence
&& > ~~ (if any).
&& >
&& > That's for a /g match *IN SCALAR CONTEXT*. In list context, it's
&& > quite different.
&& ...
&& I missing something?

I've no doubt that you know the difference between scalar and list
context. But I wanted to make sure the OP is made aware of it as well.

Got it. Thank you for the clarification.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top