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:
If so, you weren't too far off.<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>
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 | |
|
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 | |
by ww (Archbishop) on Jul 05, 2009 at 11:25 UTC | |
by web_developer (Initiate) on Jul 05, 2009 at 22:08 UTC | |
|
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 |