Your code is inefficient because of the nested loops here:
while (my $line=<$txtfile>) { foreach (@InputData_Unions)
But it is not clear whether you can easily get rid of these nested loops.

One typical way to get rid of nested loops is to store keywords in a hash and have a hash lookup instead of a sequential list search, but that doesn't quite work here (at least not easily) with your Unions list containing several words.

Depending on how large your list of unions is, you could possibly build a regex with alternates for each union, something like:

if ($line =~/Sarnia Police Association|Sarnia Professional Fire Fighte +rs|.../) { # ...
With the data you've shown, I would start by excluding any line that doesn't contain the word "Sarnia":
while (my $line = <$txtfile>) { next unless $line =~/Sarnia/; foreach (@InputData_Unions) { # ...
This would probably make your program very significantly faster, but it may be that looking for "Sarnia" is not really applicable to your real case.

My point, though, is that the more you know about your data, the more you're likely to find some improvements or shortcuts.


In reply to Re: How to efficiently search for list of strings in multiple files? by Laurent_R
in thread How to efficiently search for list of strings in multiple files? by stray_tachyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.