LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   matching the data from two text files and printing it in the form of a matrix in the (https://www.linuxquestions.org/questions/linux-newbie-8/matching-the-data-from-two-text-files-and-printing-it-in-the-form-of-a-matrix-in-the-4175453863/)

adi05 03-13-2013 12:51 AM

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;

chrism01 03-13-2013 03:26 AM

Re desired output
Code:

Jack Mac

sn1 CC AA

sn2 TT AA

sn3 TT TT

sn4 CC CC

Ignoring blank lines, I get the matching I think, except the first line; I don't see why that occurs.
Can you clarify?

adi05 03-13-2013 06:01 AM

chris..i didnt understand ..is the code i gave working?


All times are GMT -5. The time now is 07:54 AM.