Hi,
I have a file containing multiple-headed data (input file 1), and also a second file containing elements for searching the first file.
input file 1:
UROPA
sseD 1.2.3.3.3 crimson
ddsU 2.1.4.1.2 green
SAMEL
aadH 7.4.1.1.1 blue
uuoG 10.1.2.3.4 white
MOONA
gmaL 3.4.1.6.7 red
oolJ 9.1.1.4.1 yellow
input file 2:
sseD
aadH
oolJ
gmaL
My required output:
UROPA:
sseD
SAMEL:
aadH
MOONA:
oolJ
gmaL
I have the following code which is not doing what I wanted:
use strict;
use warnings;
my $time = scalar localtime();
my $heading = qq(Date of search: $time\nThe following were found:\n);
my %hash;
my %hash1;
my %hash2;
my $input1 = 'input1.txt';
#open input file1 for reading
open my $FH1, '<', $input1 or die "can't open $input1:$!";
while (<$FH1>)
{
chomp;
if (/^[A-Z]/)
{
my $Header;
$hash{$Header} = $_;
next;
}
my ($Col1, $col2, $Col3) = split /\t/, $_;
$hash1{$Col1}=$Col2;
$hash2{$Col1}=$Col3;
}
close $FH1;
my $input2 = 'input2.txt'; #read input file2
#open output file
open my $OUTPUT, '>', 'outfile.txt' or die "can't open outfile.txt:$!";
#Open input file2
open my $FH2, '<', $input2 or die "Can't open $input2:$!";
print $OUTPUT $heading;
while( defined( my $line = <$FH2> ) )
{
chomp $line;
print $OUTPUT $hash{$Header};
my $matchLine;
if ($line)
{
$matchLine = $line;
exists $hash1{$matchLine}, $hash2{$matchLine}
? print $OUTPUT $matchLine, "\t", $hash1{$matchLine}, "\t", $hash2{$matchLine}, $/
: print $OUTPUT $matchLine, "\t", "none", "\t", "none", $/;
}
}
close $OUTPUT or die "Can't close $OUTPUT:$!";
close $FH2 or die "Can't close $FH2:$!";
Can someone please point me in the right direction. Thanks