Hi,
I need help to make a perl program work. I have two files - file 1 and file 2. The contents of File2 is to be searched with the contents of file 1.
File2: 2 tab-delimited columns
XM:1120002 complex-solution
MM:0999111 blue-green solution
UX:1020022 activity unknown, (simple/complex?)
File1:(one column of space separated strings)
XM:1120002 MM:0999111 UX:1020022
UX:1020022 XM:1120002
XM:1120002
Output required: 2 tab-delimited columns
Date:
The following were found in file2:
XM:1120002 MM:0999111 UX:1020022 complex-solution;blue-green solution;activity unknown, (simple/complex?)
UX:1020022 XM:1120002 complex-solution;blue-green solution;activity unknown
XM:1120002 complex-solution
The code is as follows:
use warnings;
use strict;
my $time = scalar localtime(); # get date of search
my $heading = qq(Date of search: $time\nThe Following Matches were Found in File 2:\n);
my %hash1;
my %hash2;
my $file2 = 'file2.txt'; # file 2: file with two columns to be searched with contents of file1.
open my $fh, '<', $file2 or die "can't open $file2:$!";
while (<$fh>) {
chomp;
my ($ID, $value) = split /\s+/, $_;
$hash1{$ID} = $value;
}
close $fh;
my $file1 = 'file1.txt'; # file1: one column of probe names for searching the second file.
open my $fh_new, '>', 'ouput.txt' or die "can't open file:$!"; # Open output file for writing
my $i=();
open $fh, '<', $file1 or die "can't open this file:$!"; #Open query file for reading
print $fh_new $heading;
while ( defined( my $line = <$fh> ) ) {
chomp $line;
my ($checked_word[$i]) = split /\s+/,$line;
$hash2{$checked_word[$i]} = $checked_word[$i];
if ( $line ) {
$checked_word[$i] = $line;
exists $hash{$checked_word[$i}
? print $fh_new $checked_word[$i], "\t", $hash{$checked_word}[$i], $/
: print $fh_new $checked_word, "\t", "######", $/;
}
}
close $fh or die "can't close file:$!";
close $fh_new or die "can't close file:$!";
I will appreciate any help! Thanks