in reply to Re^6: Hash w/ multiple values + merging
in thread Hash w/ multiple values + merging

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; }