Monks,
Please help this Perl novice.
I am attempting to make 30 different matches from the input file to highlight in the output file, so I am searching for a way to automate the process, but I do not know how to create a process for Perl to identify and capitalize the chosen text.
I am wanting (if possible) to read in a reference file with the list of words I want to highlight within the main input file. Then have Perl ~HIGHLIGHT~ the matched text.
My problem is that some of the text (words) are "broken" by punctuation or spaces of an arbitrary length (as if I wanted to match ~A MY~ from the words "saw the panda myself" (using the 'a' from pand~A and the 'my' from MY~self)). It seems as though I can use Reg Exps for the single words, but for these "broken" words and for double words on a line (I want to match ~PANDA~ ~MYSELF~ in "saw the panda myself" on a single line) using reg exps with '/gi' will only select the first given match when using the '|' as a separator (i.e. '=~ /panda|myself/gi'). (Meaning the reg exp only higlights ~PANDA~ and not "myself").
I would show some of my code, but it is embarrasingly inadequate for solving this textual dilemma.
What can I do to have Perl 'intelligently' find (using the reference file) and ~HIGHLIGHT~ the "broken" and "double" words on a line? or will it be that I need to find each word myself and then hard code them into the script?
I am trying to copy the WHOLE file to a NEW file with the ~HIGHLIGHT~s in the new file. (but (I think) I just need help on the text markup part)
I apologize for my confusing description of the activity, but I am not sure how to better word it. Thank you in advance for your help and wisdom.
UPDATE:
This is the code I have come up with so far:
# Open puzzle file and print ##
open(IN, "/Users/TMP/Puz.txt") or die "Can't open Puz.txt for reading:
+ $!\n";
@words = <IN>;
foreach $word(@words) {
chomp($word);
# print "$word\n";
push(@words2, $word);
}
close(IN);
# open refernce file for words to find
open REF, "/Users/TMP/Ref_file.txt" or die "Can't open Ref_file.txt fo
+r reading: $!\n";
my @refs = <REF>;
foreach $ref(@refs) {
chomp($ref);
push(@refs2, $ref);
}
##############################
# Process array
&find_words(@words2);
# find words subroutine
sub find_words {
while(@words2) {
$words = shift(@words2);
foreach $ref(@refs2) {
if($words =~ /$ref/gi) {
print "$`","~\U$&~","$'","\n";
}
}
}
}
That's my code in a nutshell.
Thank you for all of the posts so far. They are a GREAT help!
UPDATE (FINAL):
Thank you all for the help. Here is the final code I used for this particular problem:
sub find_words {
print "Here are the answers!\n\n";
while (@input2) {
$in = shift(@input2);
$in =~ s{($regexp)}{ **\U$1\E** }g;
print "$in\n";
}
exit;
}