matching the data from two text files and printing it in the form of a matrix in the
based on column 1 & 2 of file1 and Column1 and row1 of file 2 i want to extract the value of column 3 of file1 and insert write the matrix as shown below in desired output. Files are unsorted. I am a beginner to programming.Any help in perl would be greatly appreciated. Thanks
eg1.txt
sn1 Jack CC
sn3 Jack TT
sn2 Jack TT
sn4 Jack CC
sn4 Mac CC
sn1 Mac AA
sn3 Mac TT
sn2 Mac AA
eg2.txt
Jack Mac
sn1
sn2
sn3
sn4
DESIRED OUTPUT
Jack Mac
sn1 CC AA
sn2 TT AA
sn3 TT TT
sn4 CC CC
below is my code, i'm certainly doing alot wrong here. Plz help
use warnings;
use strict;
use IO::Handle;
use FileHandle;
my %account;
$file1 = "C:\perl\bin\eg1.txt";
$file2= "C:\perl\bin\eg2.txt";
$outfile = "C:\perl\bin\out3.txt";
open( my $fh1,"<", "eg1.txt" ) or die "$!";
while( my $line = <$fh1> ) {
my @values = split ' ', $line;
$account{$values[0]} = $values[1];
print"$values[2]\n";
}
close $fh1;
open( my $out_fh, ">", "out.txt" ) or die "$!";
open( my $fh2, "<", "eg2.txt" ) or die "$!";
while( my $line = <$fh2> ) {
my @values = split ' ', $line;
print $out_fh join $values[0], $account{$values[0]}, $values[1];
#print"$out_fh\n";
}
close $out_fh;
close $fh2;
|