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;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.