Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > When is $#- != $#+ ? When is $-[$i] undef ?

Reply
Thread Tools

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

 
 
Irving Kimura
Guest
Posts: n/a
 
      05-22-2004




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

 
Reply With Quote
 
 
 
 
Lukas Mai
Guest
Posts: n/a
 
      05-22-2004
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:umper -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"
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      05-22-2004

Quoth Irving Kimura <(E-Mail Removed)>:
> 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.
http://www.velocityreviews.com/forums/(E-Mail Removed) (Kate Rusby)
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      05-22-2004
Irving Kimura <(E-Mail Removed)> 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:umper;
'foo' =~ /(x)|(foo)|(y)/;
print Dumper \@-, \@+;

outputs:

$VAR1 = [
'0',
undef,
'0'
];
$VAR2 = [
'3',
undef,
'3',
undef
];

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
undef($foo) versus $foo = undef()? Tim McDaniel Perl Misc 6 08-19-2009 08:31 AM
How to undef a typedef?? informativeguy@gmail.com C Programming 15 11-18-2006 08:26 PM
undef. behaviour in ctor-exc. handlers Klaus Ahrens C++ 6 11-29-2005 05:03 PM
#undef for function Sachin C Programming 3 11-28-2005 08:39 AM
win32 adodb/mdb; con.execute returns undef and not recordset. Vikas Yadav Perl 0 09-09-2003 09:10 AM



Advertisments