my $string = "aXbcXdefgXhijXklmnopqrXstuvwXyz"; my $target = 'X'; my @offsets; while ( $string =~ /$target/g ) { push @offsets, pos($string) - length($target); } print "@offsets\n";
Does the trick for me, and it scales to arbitrary substrings.
(Update: hoisting length($target) out of the loop is a no-brainer. Alas, I had no brain at the time.)
In reply to Re: Producing a list of offsets efficiently
by dws
in thread Producing a list of offsets efficiently
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |