9

I want to know the meaning of perl special variables $-[0] and $+[0]

I have googled and found that $- represent number of lines left on the page and $+ represent the last bracket matched by the last search pattern.

But my question is what $-[0] and $+[0] means in context of regular expressions.

Let me know if code sample is required.

Denis Ibaev
  • 2,470
  • 23
  • 29
AnonGeek
  • 7,408
  • 11
  • 39
  • 55
  • 1
    Did you read the perlvar perldoc? http://perldoc.perl.org/perlvar.html The example on `$+[0]` is quite clear. – J-16 SDiZ Jun 25 '12 at 09:53
  • You should be more familiarize with [manual][1]. – Shiplu Mokaddim Jun 25 '12 at 09:54
  • 5
    The variables you're looking for are `@-` and `@+`. – flesk Jun 25 '12 at 09:55
  • 3
    i don't know why everyone act so harsh and downvote when i ask noob questions...i have just started working on perl and obviously i was not familiar with @-..i have been given task of reading some existing code and there was no @- so how would I know about it..I have spend an hour searching for $-[0] and playing with the code to understand it myself before asking this question....is stackoverflow not for noobs? – AnonGeek Jun 25 '12 at 10:01
  • 2
    @raina77ow i searched the manual but as I mentioned I was searching for $-[0] so it gave no matching results....anyway I got your point – AnonGeek Jun 25 '12 at 10:10
  • 3
    +1 for noob support. I remember the first time I saw $_[0] in a subroutine. Had me scratching my head a bit. – Bill Ruppert Jun 25 '12 at 12:26
  • 1
    @BillRuppert thanks for the support...but I get the point of downvoters...In future I will do a more in-depth search on official perldoc before asking any question... – AnonGeek Jun 25 '12 at 13:05

3 Answers3

14

See perldoc perlvar about @+ and @-.

$+[0] is the offset into the string of the end of the entire match.

$-[0] is the offset of the start of the last successful match.

Community
  • 1
  • 1
Denis Ibaev
  • 2,470
  • 23
  • 29
8

These are both elements from an array (indicated by the square brackets and number), so you want to search for @- (the array) and not $- (an unrelated scalar variable).

The commend

perldoc perlvar 

explains Perl's special variables. If you search in there for @- you will find.

$-[0] is the offset of the start of the last successful match. $-[n] is the offset of the start of the substring matched by n-th subpattern, or undef if the subpattern did not match.

matt freake
  • 4,877
  • 4
  • 27
  • 56
6

Adding example for better understanding of $-[0],$+[0]

Also adding info on variable $+

use strict;
use warnings;

my $str="This is a Hello World program";
$str=~/Hello/;

local $\="\n"; # Used to separate output 

print $-[0]; # $-[0] is the offset of the start of the last successful match. 

print $+[0]; # $+[0] is the offset into the string of the end of the entire match. 

$str=~/(This)(.*?)Hello(.*?)program/;

print $str;

print $+;                    # This returns the last bracket result match 

Output:

D:\perlex>perl perlvar.pl
10                           # position of 'H' in str
15                           # position where match ends in str
This is a Hello World program
 World
  • 1
    thanks for the nice example..the concept of @+ @- $- $+ are completely clear to me now...but this will definitely help future visitors +1 – AnonGeek Jun 25 '12 at 17:55