in reply to Re: Re: pattern matching
in thread pattern matching

You can reduce the code quite a bit by playing to Perl's strengths. The quick way to load the story into an array (once you've opened the file) is
my @story = <FILE>;
You can reduce the amount of work you're doing to pull out bracketed words by doing
foreach my $line ( @story ) { $line =~ s/\[([A-Za-z-]+)\]/Replace($1)/eg; }
This replaces each bracketed work with whatever Replace() returns for that word. The /eg modifiers on the regular expression say to replace whatever is found with the results of executing (/e) the expression on the right-hand side, and to repeat this match "globally" (/g) across the target string. And, because $line is "aliased" to each row in @story, you don't need to make a second pass through @story.

Then, to print the story

print @story;
Now, you've localized all of the interesting work to
sub Replace { my $word = shift; ... }
which I'll leave as an exercise.