in reply to How can one create an array of the indices at which a given character appears in a string?

TMTOWTDI

Here's a way with pos() and regular expressions:

use strict; use warnings; my $input = 'rnbqkbnr'; my $search = qr/n/; my @result; push @result, pos($input) - 1 while $input =~ /$search/cg; print "@result";
  • Comment on Re: How can one create an array of the indices at which a given character appears in a string?
  • Select or Download Code