Perl Monk, Perl Meditation | |
PerlMonks |
Inconsistent behaviour of pos() in s/ ... / ... /by johngg (Canon) |
on May 10, 2006 at 22:48 UTC ( [id://548583]=perlquestion: print w/replies, xml ) | Need Help?? |
johngg has asked for the wisdom of the Perl Monks concerning the following question:
Prompted by the responses in Newbie Q:How do I compare items within a string?, in particular those of Zaxo and TedPride I had a go at expanding my attempt with some of their ideas. This lead to some confusion about the use of pos() as shown here. In order to explore this further I have written this script which attempts to find occurrences of one string within another and to annotate each occurrence with the offset (zero-based) within the string
which when run produces
As per the documentation, during the matching phase the value returned by pos() corresponds with the value of $+[1], pointing to just after the match. As Zaxo pointed out, $-[1] points to the start of the match. In the annotation stage pos() again points to just after the match in the (?{ ... }) block, where the last / .../g match left off, I think the documentation says. However, when I use pos() as part of the substitution it seems to return values corresponding to $-[1] and not $+[1]. My question is, what could be causing this apparent change in behaviour? Cheers, JohnGG
Back to
Seekers of Perl Wisdom
|
|