Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re^2: partial matching of lines in perlby AnomalousMonk (Archbishop) |
on Jun 12, 2020 at 15:29 UTC ( [id://11117987]=note: print w/replies, xml ) | Need Help?? |
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 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 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 Section
Seekers of Perl Wisdom
|
|