Name

pos

Synopsis

pos $scalar

Description

Returns the location in scalar where the last m//g search over scalar left off. It returns the offset of the character after the last one matched. This is the offset where the next m//g search on that string will start. Remember that the offset of the beginning of the string is 0. For example:

$grafitto = "fee fie foe foo";
while ($grafitto =~ m/e/g) {
    print pos $grafitto, "\n";
}

prints 2, 3, 7, and 11, the offsets of each of the characters following an e”. The pos function may be assigned a value to tell the next m//g where to start.

Get Perl in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.