Here's a variation based on index that seems to satisfy your requirement insofar as I understand it as discussed here, here and here.

Note that this solution is O(n1 * n2) (the product of the number of lines in each file) because it depends on a nested loop, whereas the regex-based solution presented by BillKSmith here is O(n). Unfortunately, the regex-based solution imposes a tighter limit on the size of the substrings file that can be supported: at least several hundred, but surely no more than several thousand substring lines. The index-based solution, while potentially much slower, can support a few, perhaps several, million lines of substrings. (Caveat: These are all estimates.) The number of lines to be searched for substrings is unlimited with both approaches if the lines are processed line-by-line in a while-loop. The code below identifies both lines that match some substring and lines that do not match any substring, so comment out whichever branch of the if-else conditional you do not need. (There's also a bit of ornamental code that highlights the substring that was found.)

c:\@Work\Perl\monks>perl use strict; use warnings; use autodie; use List::MoreUtils qw(any); # use List::Util in later perl versions my $file1 = \<<"END1"; # strings to be searched for substrings he is man xyzzy don't you what goes on END1 my $file2 = \<<"END2"; # substrings to search for he is what are z try to do END2 open my $fh_substrings, '<', $file2; my @substrings = <$fh_substrings>; chomp @substrings; close $fh_substrings; open my $fh_lines, '<', $file1; while (my $line = <$fh_lines>) { chomp $line; print "'$line' "; my $s; # matched substring in line my $o; # matched substring offset if (any { ($s = $_, $o = index($line, $_)) >= 0 } @substrings) { print "match \n"; print ' ', ' ' x $o, '^' x length $s, "\n"; } else { print "NO match \n"; } } close $fh_lines; __END__ 'he is man' match ^^^^^ 'xyzzy' match ^ 'don't you' NO match 'what goes on' NO match


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


In reply to Re^3: 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.