How does @LAST_MATCH_START work?

O

Olof Karlberg

Hi,

I am looking for a way to find the locations of exact substring matches (0.1-3 KB) in a larger string (1-2MB) and was looking at the variables @LAST_MATCH_START and @LAST_MATCH_END (@-/@+) as a solution. The problem is that I just can't get these variables to work the way I thought they should work. As I understand perdoc perlvar on this, the code below _should_ print:

Number of matches: 2
First match begins at: 2
First match ends at: 5
2:nd match begins at: 9
2:nd match ends at: 12

However, what it _does_ print is:

Number of matches: 0
First match begins at: 2
First match ends at: 5
Use of uninitialized value in concatenation (.) or string at ./test.pl line 10.
2:nd match begins at:
Use of uninitialized value in concatenation (.) or string at ./test.pl line 11.
2:nd match ends at:

#!/usr/bin/perl -w

my $target = "QWERTY QWERTY";
my $query = "ERT";

$target =~ m/$query/g;
print "Number of matches: $#-\n";
print "First match begins at: $-[0]\n";
print "First match ends at: $+[0]\n";
print "2:nd match begins at: $-[1]\n";
print "2:nd match ends at: $+[1]\n";

What is going on here? Have I totally misunderstood the docs?
The above is the result from Perl 5.8.0 on a new Gentoo Linux system (gcc version 3.2.2). The same code on Perl 5.6.1 on Suse 8.0 (gcc version 2.95.3) gives the same printout as above with the following diff:

Number of matches: 2020961897

Anyone else seen this behaviour?

//Olof
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Olof Karlberg
$target =~ m/$query/g;
print "Number of matches: $#-\n";

Some time ago the parser had a hard time groking this. I would try
$#{'-'} or some such instead.

Ilya
 
J

Johannes Fürnkranz

Olof said:
#!/usr/bin/perl -w

my $target = "QWERTY QWERTY";
my $query = "ERT";

$target =~ m/$query/g;
print "Number of matches: $#-\n";
print "First match begins at: $-[0]\n";
print "First match ends at: $+[0]\n";
print "2:nd match begins at: $-[1]\n";
print "2:nd match ends at: $+[1]\n";

What is going on here? Have I totally misunderstood the docs?

Wow. I had never seen @- and @+ before. Are these new in 5.8? Perl in a
Nutshell (2nd ed for 5.8) does not seem to carry them either, however.

Anyways: Yes, you did misunderstand the docs.
@+ and @- do not contain multiple matches, but they contain the starting
indices of all (..) expressions. Replace
$target =~ m/$query/g;
with
$target =~ m/(E)R(T)/g;
and you'll see.

What you probably want to do is the following:

my $target = "QWERTY QWERTY";
my $query = "ERT";

my $i = 0;
while($target =~ m/$query/g) {
printf "Match %d begins at %d\n", ++$i, pos $target;
}

You also want to look at the \G anchor in perlre.

There's longer code example in perlop.

cheers, Juffi
 
J

Johannes Fürnkranz

Abigail said:
&& Perl in a
&& Nutshell (2nd ed for 5.8) does not seem to carry them either, however.

I guess that says more about that book than about @- and @+.

Might be. But I haven't found them in any of my other books either (all
of them are on 5 or higher, but I'm not sure whether they are >= 5.6).

Nutshell is the only one I have which covers 5.8., which is why I
mentioned it here. This was not necessarily meant to be a recommendation.

However, having said this, I found it very useful as a general
reference. It doesn't give you more than a condensed version of perldoc,
but I prefer look-up in well-indexed books. I rarely find things in
perldoc unless I know exactly what I'm looking for.

Juffi
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Olof Karlberg
Thanks, but the result is the same. I also noted that $-[-1] ==
$-[0] which means that @- only holds one element although the string
should match twice.

Given that @- is magic, this should not mean anything (at least when
you *know* that things are not working as expected ;-).

Hope this helps,
Ilya
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top