about matching in a list context

Discussion in 'Perl Misc' started by sharma__r@hotmail.com, Oct 9, 2006.

  1. Guest

    Hello,

    I don't understand this behavior of perl:


    perl -lne 'print for /^(\d+\.\d+E-\d+)$/;' << EOF
    ***********
    4.62972E-13
    4.63098E-13
    4.62983E-13
    ***********
    EOF

    returns corrrectly --->

    4.62972E-13
    4.63098E-13
    4.62983E-13


    Whereas if I do this :

    perl -lne 'print for /^\d+\.\d+E-\d+$/;'

    I get (from the exact same input as above) :
    1
    1
    1

    Now I want to know what's going on?

    Thanks
    Rakesh
    , Oct 9, 2006
    #1
    1. Advertising

  2. Eric Amick Guest

    On 9 Oct 2006 05:14:01 -0700, wrote:

    >Hello,
    >
    >I don't understand this behavior of perl:
    >
    >
    >perl -lne 'print for /^(\d+\.\d+E-\d+)$/;' << EOF
    >***********
    >4.62972E-13
    >4.63098E-13
    >4.62983E-13
    >***********
    >EOF
    >
    >returns corrrectly --->
    >
    >4.62972E-13
    >4.63098E-13
    >4.62983E-13
    >
    >
    >Whereas if I do this :
    >
    >perl -lne 'print for /^\d+\.\d+E-\d+$/;'
    >
    >I get (from the exact same input as above) :
    >1
    >1
    >1
    >
    >Now I want to know what's going on?


    Here's the relevant passage from "Regexp Quote-Like Operators" in
    perlop:

    If the /g option is not used, m// in list context returns a list
    consisting of the subexpressions matched by the parentheses in the
    pattern, i.e., ($1, $2, $3...). (Note that here $1 etc. are also set,
    and that this differs from Perl 4's behavior.) When there are no
    parentheses in the pattern, the return value is the list (1) for
    success. With or without parentheses, an empty list is returned upon
    failure.

    (Remember that the foreach modifier, like the foreach loop, imposes list
    context.) If you want the second example to work the same as the first,
    add the /g option.
    --
    Eric Amick
    Columbia, MD
    Eric Amick, Oct 9, 2006
    #2
    1. Advertising

  3. Paul Lalli Guest

    wrote:

    > I don't understand this behavior of perl:
    >
    >
    > perl -lne 'print for /^(\d+\.\d+E-\d+)$/;' << EOF
    > ***********
    > 4.62972E-13
    > 4.63098E-13
    > 4.62983E-13
    > ***********
    > EOF
    >
    > returns corrrectly --->
    >
    > 4.62972E-13
    > 4.63098E-13
    > 4.62983E-13
    >
    >
    > Whereas if I do this :
    >
    > perl -lne 'print for /^\d+\.\d+E-\d+$/;'
    >
    > I get (from the exact same input as above) :
    > 1
    > 1
    > 1
    >
    > Now I want to know what's going on?


    In a list context, a pattern match returns a list of all the captured
    sub-matches contained within parentheses. If there are no parentheses
    within the pattern, the pattern match returns the list containing 1.

    In your first pattern, you've captured the entire pattern - whatever
    matches (\d+\.\d+E-\d+) will be returned. So for each line of input
    that matches the pattern, that captured sub-pattern is returned. In
    the second pattern, nothing is captured, and so the pattern returns 1
    for each line of text that matches the pattern.

    This behavior is documented in `perldoc perlop`. Search for
    "m/PATTERN/".

    Paul Lalli
    Paul Lalli, Oct 9, 2006
    #3
    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. VS_NET_DEV
    Replies:
    2
    Views:
    3,778
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    681
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Flip Rayner
    Replies:
    1
    Views:
    632
    bruce barker
    Jan 23, 2007
  4. asd
    Replies:
    1
    Views:
    416
    www.pulpjava.com
    Nov 9, 2006
  5. Bo Lindbergh

    List context versus list context

    Bo Lindbergh, Jun 21, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    208
    Charles DeRykus
    Jun 28, 2006
Loading...

Share This Page