When is $#- != $#+ ? When is $-[$i] undef ?

Discussion in 'Perl Misc' started by Irving Kimura, May 22, 2004.

  1. The documentation for @- in perlvar says

    One can use "$#-" to find the last matched subgroup in the last
    successful match. Contrast with $#+, the number of subgroups
    in the regular expression.

    ....which suggests that it is possible for $#- and $#+ to be different,
    but I have not been able to produce an example in which they are
    actually different. In fact, later in the documentation for @-,
    perlvar says

    After a match against some variable $var:

    $` is the same as "substr($var, 0, $-[0])"
    $& is the same as "substr($var, $-[0], $+[0] - $-[0])"
    $' is the same as "substr($var, $+[0])"
    $1 is the same as "substr($var, $-[1], $+[1] - $-[1])"
    $2 is the same as "substr($var, $-[2], $+[2] - $-[2])"
    $3 is the same as "substr $var, $-[3], $+[3] - $-[3])"

    ....which implies that $-[n] is defined if and only if $+[n] is
    defined, meaning that $#- and $#+ should always be equal. What
    gives?

    In addition, the above quote suggests that all elements of @- are
    defined (if some element of @- were undefined, the corresponding
    substr expression would be nonsensical), but the docs say that
    there are cases in which some elements of @- are undefined. Although
    I've managed to come up with examples in which, for some n, $-[n]
    and $+[n] are equal (implying an "empty submatch"), I have not been
    able to produce an example in which @- has undefined elements.

    But more important than an example is the answer to this question:
    which regexps can produce empty submatches (both $-[n] and $+[n]
    are defined and equal), and which can produce "undefined" ones
    (both $-[n] and $+[n] are undefined)? Can the same parenthesized
    group in a regexp produce either an empty submatch or an undefined
    one depending on the input string?

    Thanks!

    Irv
     
    Irving Kimura, May 22, 2004
    #1
    1. Advertising

  2. Irving Kimura

    Lukas Mai Guest

    Irving Kimura schrob:
    > The documentation for @- in perlvar says


    > One can use "$#-" to find the last matched subgroup in the last
    > successful match. Contrast with $#+, the number of subgroups
    > in the regular expression.


    > ...which suggests that it is possible for $#- and $#+ to be different,
    > but I have not been able to produce an example in which they are
    > actually different.

    [...]

    Here's an example where they're different:
    $ perl -wle '"a" =~ /a|(x)/; print "$#- $#+"'
    0 1

    And here's an example with undefined elements in @-:
    $ perl -MData::Dumper -we '"x" =~ /(a)|(x)|(b)/; print "$#- $#+\n", Dumper(\@-, \@+)'
    2 3
    $VAR1 = [
    '0',
    undef,
    '0'
    ];
    $VAR2 = [
    '1',
    undef,
    '1',
    undef
    ];

    HTH, Lukas
    --
    perl -eval "-MPerlIO=via;printf'Just another Perl hacker%c '=>44"
     
    Lukas Mai, May 22, 2004
    #2
    1. Advertising

  3. Irving Kimura

    Ben Morrow Guest

    Quoth Irving Kimura <>:
    > In fact, later in the documentation for @-,
    > perlvar says

    <schtuff>
    > ...which implies that $-[n] is defined if and only if $+[n] is
    > defined, meaning that $#- and $#+ should always be equal. What gives?
    >

    <snip>
    > I have not been able to produce an example in which @- has undefined
    > elements.


    A paren group which matches the empty string produces equal elements of
    @+ and @-. A paren group which is not matched at all produces undefined
    elements, or in the case of final elements of @-, no element at all.

    Consider

    $ perl -le'"foo" =~ /(bar)|fo()o|(baz)/;
    print join ",", map { defined $_ ? $_ : "(undef)" } @-, ":", @+'
    0,(undef),2,:,3,(undef),2,(undef)

    .. I think that's got all the cases... :)

    Ben

    --
    If I were a butterfly I'd live for a day, / I would be free, just blowing away.
    This cruel country has driven me down / Teased me and lied, teased me and lied.
    I've only sad stories to tell to this town: / My dreams have withered and died.
    (Kate Rusby)
     
    Ben Morrow, May 22, 2004
    #3
  4. Irving Kimura

    Jay Tilton Guest

    Irving Kimura <> wrote:

    : The documentation for @- in perlvar says
    :
    : One can use "$#-" to find the last matched subgroup in the last
    : successful match. Contrast with $#+, the number of subgroups
    : in the regular expression.
    :
    : ...which suggests that it is possible for $#- and $#+ to be different,
    : but I have not been able to produce an example in which they are
    : actually different.

    [...]

    : the docs say that
    : there are cases in which some elements of @- are undefined. Although
    : I've managed to come up with examples in which, for some n, $-[n]
    : and $+[n] are equal (implying an "empty submatch"), I have not been
    : able to produce an example in which @- has undefined elements.

    An example that demonstrates both phenomena:

    #!perl
    use Data::Dumper;
    'foo' =~ /(x)|(foo)|(y)/;
    print Dumper \@-, \@+;

    outputs:

    $VAR1 = [
    '0',
    undef,
    '0'
    ];
    $VAR2 = [
    '3',
    undef,
    '3',
    undef
    ];
     
    Jay Tilton, May 22, 2004
    #4
    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. Vikas Yadav
    Replies:
    0
    Views:
    1,156
    Vikas Yadav
    Sep 9, 2003
  2. Sachin

    #undef for function

    Sachin, Nov 25, 2005, in forum: C Programming
    Replies:
    3
    Views:
    434
    Peter Ammon
    Nov 28, 2005
  3. How to undef a typedef??

    , Nov 16, 2006, in forum: C Programming
    Replies:
    15
    Views:
    1,377
    Kenneth Brody
    Nov 18, 2006
  4. Klaus Ahrens
    Replies:
    6
    Views:
    396
    Alf P. Steinbach
    Nov 29, 2005
  5. Tim McDaniel

    undef($foo) versus $foo = undef()?

    Tim McDaniel, Aug 19, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    163
    Peter J. Holzer
    Aug 19, 2009
Loading...

Share This Page