First of all, while I don't know in this case if this is homework, the way your post is written sounds like some homework-like questions I have seen before. Most people here have already done their share of homework, and so may be a little reluctant to do someone else's as well. Show what you've tried so far, or even how you think you might, and you will likely get more/better results in the future. That having been said, now back to our regularly-scheduled post.

If the search list is short enough, you can store the items as keys in a hash, then loop thru general data file and push the line numbers into the hash when found, and when done list the entries found. Something like the following (untested) code might work...

open(SF, $searchfile) or die(qq{Can't open $searchfile for input: $!\n}); while (<SF>) { chomp; @{$results{$_}} = (); } close(SF); open(DF, $datafile) or die(qq{Can't open $datafile for input: $!\n}); while (<DF>) { foreach my $k (keys %results) { if (m/$k/) { push(@{$results{$k}}, $.); last; } } } close(DF); foreach my $k (sort keys %results) { if (scalar @{$results{$k}}) { printf "%s:\n\t%s\n", $k, join(",", sort @{$results{$k}}); } }

Hope that helps.


In reply to Re: compare two text files by atcroft
in thread compare two text files by juergenkemeter

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.