in reply to Line by line parsing from one file, comparing line by line to another file

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:

Replies are listed 'Best First'.
Re^2: Line by line parsing from one file, comparing line by line to another file
by jwkrahn (Abbot) on Jul 05, 2009 at 04:45 UTC
    my $names_file = "code.txt"; open(NAMES, $names_file || die "Could not open file!");

    Because of the high precedence of the  || operator die will never be called because $names_file is always true.

    You need to either use parentheses in the proper place:

    open(NAMES, $names_file) || die "Could not open file!";

    Or use the lower precedence  or operator:

    open NAMES, $names_file or die "Could not open file!";
Re^2: Line by line parsing from one file, comparing line by line to another file
by web_developer (Initiate) on Jul 05, 2009 at 05:20 UTC
    I atempted a modified version of your script and got this:
    $ perl.exe perl2.pl
    Global symbol "@data" requires explicit package name at perl2.pl line 27.
    Can't find string terminator "|" anywhere before EOF at perl2.pl line 27.

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

      The message you cite is about push @data, qq | > $names.html "\n"; is about the missing closing delimiter ( "|" ) for qq (your lack of code tags forced me to count. Meh!) But that's not the whole problem by a long shot.

      If you really want to learn Perl, "cargo-culting" (which is what you appear to be doing) is a poor course of instruction. Instead, suggest you start working your way through Learning Perl; the Tutorials here; from http://www.perltraining.com.au or from any one of several very good introductory courses available on-line (your pick, but those from colleges and universites, .edu, are sometimes better than the [very low] average web site).

        Thank you.
Re^2: Line by line parsing from one file, comparing line by line to another file
by web_developer (Initiate) on Jul 05, 2009 at 04:18 UTC
    YES!
    ===========UPDATE============
    July 4, 2009 12:09:22 AM EST
    for example, file1 line 1 has 12345 for example,
    file2 has .<html>...... 12345..</html>
    on line 1. I need line 1 of file2 to
    be made into a new file called 12345.html
    ==========UPDATE===========
    July 4, 2009 12:14:56 AM EST
    Now file2 is data_file array,
    file1 is names_file array and
    the results being several unique (names).html
    files.