What does perl special variable $-[0] and $+[0] means
Asked Answered
T

3

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.

Tina answered 25/6, 2012 at 9:48 Comment(7)
Did you read the perlvar perldoc? perldoc.perl.org/perlvar.html The example on $+[0] is quite clear.Bobbobb
You should be more familiarize with [manual][1].Pushup
The variables you're looking for are @- and @+.Aikido
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?Tina
@raina77ow i searched the manual but as I mentioned I was searching for $-[0] so it gave no matching results....anyway I got your pointTina
+1 for noob support. I remember the first time I saw $_[0] in a subroutine. Had me scratching my head a bit.Recondition
@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...Tina
W
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.

Weintrob answered 25/6, 2012 at 9:55 Comment(0)
B
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.

Bozcaada answered 25/6, 2012 at 9:55 Comment(0)
L
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
Ladyfinger answered 25/6, 2012 at 13:37 Comment(1)
thanks for the nice example..the concept of @+ @- $- $+ are completely clear to me now...but this will definitely help future visitors +1Tina

© 2022 - 2024 — McMap. All rights reserved.