Is this what you need? Is the content of the two source files something akin to what you're working with?

#!/usr/bin/perl use strict; use warnings; # 777297 my $data_file = "test1.txt"; open(DATA, $data_file) || die("Could not open file!"); my @data_file = <DATA>; my $names_file = "code.txt"; open(NAMES, $names_file || die "Could not open file!"); my @names_data = <NAMES>; # create loop that reads each ID in code.txt (NAMES array), searches f +or each in array elements for #test1.txt # (DATA array), redirects a new (NAMES).html for each element my ($names, $data); for $names(@names_data) { chomp($names); for $data(@data_file) { chomp ($data); if ($data =~ /$names/) { print "found \$data ( $data ) in \$names \n"; } } } close NAMES; close DATA;

test1.txt:

12345 34567 89246 54321 98765

code.txt:

<html> <head> <title "777297 code"</title> </head> <body> <p><span class="b">12345 </span> foobar</p> <p><span class="b">54321 </span></p> <ul><li>89246</li></ul> <div id="second">34567 <br>78912 but this one ain't there</div> </body> </html>
If so, you weren't too far off.

But please, when you post code, use <code>...</code> (or <c>...</c> tags) around code and data.

Update, in light of OP's update:
The above produces this output:

found $data ( 12345 ) in $names found $data ( 34567 ) in $names found $data ( 89246 ) in $names found $data ( 54321 ) in $names found $data ( 98765 ) in $names

So now you need to add the appropriate code to create a new file for each printed line of output and print the content of names therein.

And two BTW's:


In reply to Re: Line by line parsing from one file, comparing line by line to another file by ww
in thread Line by line parsing from one file, comparing line by line to another file by web_developer

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.