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: <%-{-{-{-<
|