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

Discussion in 'Perl Misc' started by Allan M. Due, Nov 12, 2005.

  1. Allan M. Due

    Allan M. Due Guest

    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

    --
    My Sig is a P228
    To contact me use: amd1, then the at sign, then xbarx, then obligatory dot
    and then com; or just click here = http://xbarx.com/m/amd1
     
    Allan M. Due, Nov 12, 2005
    #1
    1. Advertising

  2. "Allan M. Due" <> wrote in
    news::

    > 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";
    }

    > --
    > My Sig is a P228


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

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 12, 2005
    #2
    1. Advertising

  3. Allan M. Due

    Allan M. Due Guest

    A. Sinan Unur wrote:

    <snip excellent clarification and code suggestion>

    Thanks for clearling that up for me, much appreciated.

    >> --
    >> My Sig is a P228

    >
    > 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.

    --
    My Sig is a P228
    To contact me use: amd1, then the at sign, then xbarx, then obligatory
    dot and then com; or just click here = http://xbarx.com/m/amd1
     
    Allan M. Due, Nov 12, 2005
    #3
  4. "Allan M. Due" <> wrote in news:3tn96iFtknp6U1
    @individual.net:

    > A. Sinan Unur wrote:
    >
    > <snip excellent clarification and code suggestion>
    >
    > Thanks for clearling that up for me, much appreciated.


    You are welcome.

    >> ... 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.


    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. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 12, 2005
    #4
  5. Abigail <> wrote in
    news::

    > A. Sinan Unur () wrote on MMMMCDLVI September
    > MCMXCIII in <URL:news:Xns970CB121219BFasu1cornelledu@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.


    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. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 12, 2005
    #5
  6. Abigail <> wrote in
    news::

    > A. Sinan Unur () wrote on MMMMCDLVI September
    > MCMXCIII in <URL:news:Xns970CB99E5B8E5asu1cornelledu@127.0.0.1>:
    > && Abigail <> wrote in
    > && news::
    > &&
    > && > A. Sinan Unur () wrote on MMMMCDLVI
    > && > September MCMXCIII in <URL:news:Xns970CB121219BFasu1cornelledu@
    > && > 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
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 13, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    640
  2. BCC

    Pattern match question

    BCC, Aug 23, 2004, in forum: C++
    Replies:
    6
    Views:
    423
    Alex Vinokur
    Aug 24, 2004
  3. Chris

    Pattern Match Question

    Chris, Jun 4, 2008, in forum: Java
    Replies:
    5
    Views:
    391
    Roedy Green
    Jun 4, 2008
  4. Rodney

    Pattern Match Question..

    Rodney, Sep 13, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    95
    Rodney
    Sep 14, 2003
  5. Replies:
    5
    Views:
    142
    Tad McClellan
    Jan 15, 2005
Loading...

Share This Page