title: Perl: Search text file for keywords from array
body: <p>How do I use keywords from an array in an regex to search a files.</p>
  
  <p>I'm trying to look at a text file and see if and where the keywords appear. There are two files keywords.txt </p>
  
  <pre><code>keyword.txt
  word1
  word2
  word3
  
  filestosearchon.txt
  a lot of words that go on and one and contain linebreaks and linebreaks (up to 100000   characters)
  </code></pre>
  
  <p>I would like to find the keyword and the position of the match. This works for one word but I am unable to figure out how to iterate the keywords on the regex. </p>
  
  <pre><code>#!/usr/bin/perl
  
  # open profanity list
  open(FILE, "keywords.txt") or die("Unable to open file");
  @keywords = &lt;FILE&gt;; 
  close(FILE);
  
  # open text file
  local $/=undef; 
  open(txt, "filetosearchon.txt") or die("Unable to open file");
  $txt = &lt;txt&gt;;
  
  $regex = "keyword";
  
  
  push @section,[length($`),length($&amp;),$1]    
  while ($txt =~ m/$regex/g);
  
  foreach $element(@section)  
  {
  print (join(", ",@$element), $regex, "\n");    
  }
  </code></pre>
  
  <p>How can I iterate the keywords from the array over this while loop to get the matched keywords and position?</p>
  
  <p>Appreciate anyhelp. Thanks</p>