in reply to How to run index on this one?
A solution that uses index and substr.
$ perl -le ' > $str = q{YYSFTVMETDPVN[115]HMVGVISVEGRPGLFWFN[115]ISGGDKN[115]}; > $posn = -1; > while ( ( $posn = index $str, q{N[115]}, $posn ) > -1 ) > { > print +( $posn ++ ); > substr $str, $posn, 5, q{}; > }' 12 30 37 $
Making the solution more general and avoiding modifying the original string.
$ perl -Mstrict -wle ' > print for findPosns( > q{YYSFTVMETDPVN[115]HMVGVISVEGRPGLFWFN[115]ISGGDKN[115]}, > q{N}, > q{[115]} > ); > > sub findPosns > { > my ( $str, $textKeep, $textThrow ) = @_; > > my $text = $textKeep . $textThrow; > my $len = length $textThrow; > my @posns = (); > my $posn = -1; > while ( ( $posn = index $str, $text, $posn ) > -1 ) > { > push @posns, $posn ++; > substr $str, $posn, $len, q{}; > } > return @posns; > }' 12 30 37 $
I hope this is useful.
Cheers,
JohnGG
|
---|