cranberry13 has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I wrote a program in perl to footnote a file .... the program has a list of words that need to be footnoted and a file that the program searches through.

The problem is this..... The program is footnoting some of the words that it finds in the actual 'footnote' text.

Here is an example of the file:

$line = " Jack and jill went up the hill to fetch a pail of water. Jac +k fell down and broke his crown and jill came tumbling after."; $newline = $line; ## MAKE A COPY OF THE LINE @wordlist = (hill,pail,water,down); # THE LIST OF WORDS TO SEARCH FO +R ## FOR EACH WORD IN THE WORDLIST -- FOOTNOTE THE WORD for ($i=0; $i < @wordlist; $i++) { $currword = $wordlist[$i]; $newline =~ s/(\b$currword(?:ed|ing|al)?\b)/\\footnote\{$1 $hash{l +c($1)}{explanation}\}/im; }

And  $hash{lc($1)}{explanation} successfully returns the explanation of the current word in @wordlist.

Suppose the definition of 'pail' is:

'A cylindrical vessel with a handle that holds water.'

After my program runs, both the 'water' in the poem and the 'water' in the footnote definition of 'pail' will be footnote (ie. There is a footnote within a footnote)

How do I get it to not footnote the 'water' in the 'pail' definition?

Replies are listed 'Best First'.
Re: Need to modify my reg-expression
by Abigail-II (Bishop) on Nov 06, 2003 at 17:42 UTC
    How do I get it to not footnote the 'water' in the 'pail' definition?
    By using a single substitution. Something like this (untested)
    my $words = join "|" => @wordlist; $newline =~ s/\b((?:$words)(?:ed|ing|al))\b/\\footnote{$1 $hash{lc($1) +}{explanation}}/i; # No /m needed.

    Abigail

Re: Need to modify my reg-expression
by flounder99 (Friar) on Nov 06, 2003 at 20:57 UTC
    If none of your text or explainations contain brackets you can skip all bracketed text.
    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 ho +lds 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 \\footn +ote \{$2: $hash{lc($2)}{explanation}\}/xi; } print $newline; __END__ Outputs: Jack and jill went up the hill to fetch a pail \footnote {pail: A cyl +indrical 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.

    --

    flounder

Re: Need to modify my reg-expression
by delirium (Chaplain) on Nov 06, 2003 at 18:30 UTC
    Put in markers on the first pass, then replace the markers on a second pass:

    $newline =~ s/(\b$currword(?:ed|ing|al)?\b)/\$hashme\{$1\}/ig for @wor +dlist; $newline =~ s/\$hashme\{(.+?)\}/\\footnote\{$1 $hash{lc($1)}{explanati +on}\}/g