Thanks. I'll whip up a benchmark with some of the different approaches, (actually have, but don't have yours in yet. Though I'll probably switch the filehandle use.
Also, I'd probably use a hash instead of a hashref for $words as it is
slightly faster.
Not sure if it's faster or not (though "feels" it), I'd probably rewrite the following to use a hash slice
# before
if ($word =~ /$expr/) {
for my $j (-5 .. 5) {
$words->{$i + $j}++;
}
};
# after
my $mwords = 5;
@words{$i-$mwords..$i+$mwords} = 1
if $word =~ /$expr/;
# Note: Keys created, but all but first have undef values