in reply to Finding word either side of a word match

In addition the big picture issues moritz talks about, here are a couple of little things to think about:

@theseWords = keys %Line; @theseWords = sort @theseWords; foreach $word ( @theseWords ) {

You don't have to create a temporary array for each step here, you can just write:

foreach my $word ( sort keys @theseWords) {

Also, it looks like you've slurped the whole file into an array of lines at the beginning, and for large files you'd probably be better off just processing directly line by line.