Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pattern matching between two files 2

Status
Not open for further replies.

ailse

Programmer
Jan 20, 2004
79
GB
I have two text files, one of which is a list of names and the other is the "main" file. What I am trying to do is read both files into arrays to that I can identify lines in the "main" file that contain any of the names in my list, and then return those lines from the main file.

For example, one name is "pectinesterase". One line in the "main" text file is:

<species id="pectinesterase" compartment="default">

so I'd need to return the whole line.

so far I have:

Code:
my @names = <NAMES>;
my @source = <MAIN>;

foreach my $source(@source) {
 foreach my $name(@names) {
  if ($source =~ $name) {
     print OUTFILE $source . "\n";
     }
   }
}

but that's not giving exactly the result i'm after... any hints would be brilliant thanks :)
 
Code:
if ($source =~ /$name/) {

Your $name variable will still have a newline in it (which isn't the line in $source). You'll have to chomp that variable to remove it first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top