Richard Lee wrote:
John W. Krahn wrote:
Richard Lee wrote:
While reading 'mastering perl', I run into @- and @+ for the first time.
perldoc perlvar
Trying to understand what's going on, I ran the code from the book, but
$-[1] and $+[1] shoudln't match only the first match? (in this case,
shoudln't it be, 7 to 8 ?, instead of 7 to 9 ?)
The length of the first match is 2. 8 - 7 = 1. If $+[1] had the
value 8 then the first match would only have a length of 1.
Look at the @LAST_MATCH_START entry in:
perldoc perlvar
Also read the entries for $<digits>, $MATCH, $PREMATCH, $POSTMATCH,
$LAST_PAREN_MATCH, $^N, @LAST_MATCH_END and $LAST_REGEXP_CODE_RESULT.
And of course the regular expression documentation:
perldoc perlrequick
perldoc perlretut
perldoc perlre
perldoc perlop
John
Thanks John,
According to the doc,
since $1 is substr($var, $-[1], $+[1] - $-[1] )
and hi matches 7 and 8th elements of the variable and length has to be
2
Yes, it matches the 7th and 8th characters, but the start of the match
is after the 7th character and the end is after the 9th character
so
>
substr($var, 7, 2), but I still don't understand
because if formula was applied directly, it still would be,
substr($var, 7, (8-7)) which would come out to
substr($var, 7, 1).
What do you mean by, "if formula was applied directly"? The values of
$-[1] and $+[1] are 7 and 9, so this evaluates to
substr $var, 7, 2
so, how I am reading it is then, $+[1] is not a value that I read off
the variable but something perl assigns it to it the way it see fit?(in
order to match the first match $1)
>
and since lenght of the match is 2, $-[1] is fixed(where the match
starts) but $+[1] tries to match the length?
>
@+ definition does mentions $+[1] is hte offset past where $1 ends.
Perhaps it would help to think of the offset as being the index of the
points between the characters, so the start of the string is at offset
zero, after 'a' (and before 'b') is at offset one and so on. Then can
you see how offset 7 is before 'hi' and offset 9 is after it?
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/