How does @LAST_MATCH_START work?

Discussion in 'Perl Misc' started by Olof Karlberg, Jun 27, 2003.

  1. 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
     
    Olof Karlberg, Jun 27, 2003
    #1
    1. Advertising

  2. [A complimentary Cc of this posting was sent to
    Olof Karlberg
    <Olof.KarlbergNOSPAMat.ebc.uu.se>], who wrote in article <>:
    > $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
     
    Ilya Zakharevich, Jun 27, 2003
    #2
    1. Advertising

  3. Olof Karlberg wrote:

    > #!/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
     
    Johannes Fürnkranz, Jun 27, 2003
    #3
  4. Re: Nutshell

    Abigail wrote:
    >
    > && 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
     
    Johannes Fürnkranz, Jun 27, 2003
    #4
  5. [A complimentary Cc of this posting was sent to
    Olof Karlberg
    <Olof.KarlbergNOSPAMat.ebc.uu.se>], who wrote in article <>:
    > > Some time ago the parser had a hard time groking this. I would try
    > > $#{'-'} or some such instead.


    > 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
     
    Ilya Zakharevich, Jun 28, 2003
    #5
    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. TB
    Replies:
    2
    Views:
    3,772
  2. Alek Nazarian
    Replies:
    7
    Views:
    3,264
    Alek Nazarian
    Oct 22, 2003
  3. Nick L
    Replies:
    10
    Views:
    609
    Jerry Coffin
    Aug 31, 2004
  4. Vineet Jain
    Replies:
    9
    Views:
    462
    Fredrik Lundh
    Apr 16, 2004
  5. jblazi
    Replies:
    5
    Views:
    451
    jblazi
    Aug 16, 2004
Loading...

Share This Page