foreach $a1(@a1){ while(@a2 = <F2>) { ... } }

Note also that with the block structure quoted above, the  F2 filehandle in the nested while-loop will be "exhausted" after handling the first item in the the foreach-loop and will thereafter, I think, assign to the  @a2 array a list consisting of a single undef value. To be useful, the  F2 filehandle would need to be rewound after each pass through the while-loop; see seek. (bliako has already made this point here.)

A marginally better loop nesting structure would be

while (my $line2 = <F2>) { foreach my $line1 (@a1) { print $line1 if line2_appears_in_line1($line2, $line1); } }
However, this approach still requires a complete pass through  @a1 for every line in the  F2 file, i.e., it's still O(n1 * n2).

Another point. If you want to find out if a line from file2 (always remember to chomp this line!) is exactly present within a line from file1, the comparison should be
    if ($line1 =~ /\Q$line2_chomped\E/) { ... }
or better still (because simpler and faster)
    if (index($line1, $line2_chomped) >= 0) { ... }
See index. (index is more appropriate here because no real regex matching seems needed, only an exact substring match.) Your code here seems to have this relationship wackbards.

A more advanced point. If file file2 is small enough, the technique described in haukex's Building Regex Alternations Dynamically article could be used to build a single regex that could be matched against each line of file file1 to determine which of these lines were to be printed. This approach would require only a single pass through each file, i.e., will be O(n), but will fail if file2 is much more than several hundred (or perhaps several thousand — YMMV) lines. This approach is capable of handling an unlimited number of lines in file1 however.

Update: Minor wording and spelling corrections.


Give a man a fish:  <%-{-{-{-<


In reply to Re^2: partial matching of lines in perl by AnomalousMonk
in thread partial matching of lines in perl by Sidd@786

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.