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

I

Irving Kimura

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
 
L

Lukas Mai

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
 
B

Ben Morrow

Quoth Irving Kimura said:
In fact, later in the documentation for @-,
perlvar says
...which implies that $-[n] is defined if and only if $+[n] is
defined, meaning that $#- and $#+ should always be equal. What gives?
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
 
J

Jay Tilton

: 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
];
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top