I thought to play with the $header, but would not it cause problems? If I remove it, then the whole script would treat it just another key-value pair, and since it does not have a match on the second file, it will be dropped.
Yes, you are right! However, the (real) second file does not include any column titles at all!
file 1:
id val1 val2 val3
452 sdfdf sfgfdg asfa
154 afa afafe rghreh
161 aafa gte fdhd
file 2: (one empty line - no header)
452 kmfgkmg
213 adfadfa
997 afdafa
161 adaadaada
so the result file should capture the column titles from the first file while the second file acts only as to make possible to match and then capture the relevant new value.
resulting file:
id val1 val2 val3
452 sdfdf sfgfdg asfa kmfgkmg
161 aafa gte fdhd adaadaada
while of course i can introduce a header to the second file as such id val4 | [reply] [d/l] [select] |
I have not still found a way to keep the header ($header).
The resulting file did not include the second line from the first file (right under the header). And there is "!" sign at the beginning of the first line. I do not know yet what is the trick exactly, but it worked when I removed that exclamation mark. (It is great that I learn something new (though, not thoroughly) every time I compile and try to run the code =))
Nevertheless, I have not still managed to keep the header. I would appreciate any insight on how to modify the $header.
PS. By the way, I tried to filled in the first line of the second file (which was originally empty) as such its first element (key) matches the key from the first file... but, nope, it did not work.
Here is the code:
#!/usr/bin/perl -w
use strict;
my $data1 = "/PRBB/Data1.expr";
my $data2 = "/PRBB/Data2.acc";
my $data3 = "/PRBB/onuralp.txt";
my (%data, @columnNames);
open my $in, '<', $data1 or die "Unable to open $data1: $!";
push @columnNames, parseFile (\%data, $in);
close $in;
open my $in2, '<', $data2 or die "Unable to open $data2: $!";
push @columnNames, parseFile (\%data, $in2);
close $in2;
my $format = (('%-9s ') x (@columnNames + 1)) . "\n";
open my $out, '>', $data3 or die "Unable to open $data3: $!";
for my $key (sort keys %data) {
next if keys %{$data{$key}} != @columnNames;
printf $out $format, $key, @{$data{$key}}{@columnNames};
}
sub parseFile {
my ($dataRef, $inFile) = @_;
my $header = <$inFile>;
my ($keyColumn, @columns) = map {chomp; split} $header;
while (defined (my $line = <$inFile>)) {
chomp $line;
my ($key, @data) = split /\s+/, $line;
@{$dataRef->{$key}}{@columns} = @data;
}
return @columns;
}
| [reply] [d/l] |