Hi,
I would like to the code below to search the contents of file1 in file2 and then output results arranged according to file1 contents. At the moment the code outputs are according to the order of elemnts in file2. I need some help to get this code working as expected.
File 1:
Xm36378merp_ax
sm3722bsb223_am
S23p1234mxm_am
xxx56sssssxxm_au
File 2:
S23p1234mxm_am sjtu
Xm36378merp_ax mxth
sc373939bnss_at mmsx
we223n23g23w_aj jutx
sm3722bsb223_am sdcnm
Output
Xm36378merp_ax mxth
sm3722bsb223_am sdcnm
S23p1234mxm_am sjtu
xxx56sssssxxm_au Nothing found
working code:
use warnings;
use strict;
my $time = scalar localtime(); # get date of search
my $heading = <<"HOF";
Date of search: $time
The Following Matches were Found in File 1:
HOF
my $heading = qq(Date of search: $time\nThe Following Matches were Found in File 1:\n);
my %hash;
my $file1 = 'File1.txt'; # file1: one column of probe names for searching the second file.
open my $fh, '<', $file1 or die "can't open $file1:$!";
while (<$fh>) {
chomp;
undef $hash{$_};
}
close $fh;
my $file2 = 'File2.txtt'; # file 2: file with two columns to be searched with contents of file1.
open my $fh_new, '>', 'outFile.txt' or die "can't open file:$!"; # output file
open $fh, '<', $file2 or die "can't open this file:$!";
print $fh_new $heading;
while ( defined( my $line = <$fh> ) ) {
chomp $line;
my $checked_word;
if ( $line =~ m/(.+?)\s+?.+?$/ ) {
$checked_word = $1;
exists $hash{$checked_word}
? print $fh_new $line, $/
: print $fh_new $checked_word, " No Match Found", $/;
}
}
close $fh or die "can't close file:$!";
close $fh_new or die "can't close file:$!";
Thanks for your help