use strict; my $line = " Jack and jill went up the hill to fetch a pail of water. Jack fell down and broke his crown and jill came tumbling after."; my $newline = $line; # MAKE A COPY OF THE LINE my @wordlist = ('pail', 'water'); # THE LIST OF WORDS TO SEARCH FOR my %hash; $hash{water}{explanation} = 'A clear liquid.'; $hash{pail}{explanation} = 'A cylindrical vessel with a handle that holds water.'; ## FOR EACH WORD IN THE WORDLIST -- FOOTNOTE THE WORD for (my $i=0; $i < @wordlist; $i++) { my $currword = $wordlist[$i]; $newline =~ s/((?:[^{}]*?|\{.*?\})*)(\b$currword(?:ed|ing|al)?\b)/$1$2 \\footnote \{$2: $hash{lc($2)}{explanation}\}/xi; } print $newline; __END__ Outputs: Jack and jill went up the hill to fetch a pail \footnote {pail: A cylindrical vessel with a handle that holds water.} of water \footnote {water: A clear liquid.}. Jack fell down and broke his crown and jill came tumbling after.