I was going to include the generalized version of your solution with
my vec solution, as they're functionally fairly similar, but I couldn't get it to work. Finally found my mistake, so here's a solution to the exercise:
sub score {
my ($str, $array) = @_;
my $vec = "\0" x length($str);
for (@$array) {
my $idx = index $str, $_;
# Matching substrings are padded into position with nulls
$vec |= ("\0" x $idx) . $_;
}
# Matching characters become nulls; others non-nulls
$vec ^= $str;
# Count nulls
$vec =~ tr/\0//;
}
Update:
An interesting (possibly quite useful) thing for the OP to note is that the vec solution effectively builds the list of dots as its vector (ones in matched positions), and your solution gives one string with the actual matched characters in position.
Caution: Contents may have been coded under pressure.