Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How to merge the contents of Multiple files stored in the Hash and generate the merged file output along with Header in tabular form

For example, i have 2 files (File1.txt and File2.txt) and generate the present the output with Header Information as below

I was able to present the merged file but without header information :(

Header information i am missing to put is "Distance Fuel_Consumption_Curr Fuel_Consumption_Baseline1" in the output generated as files will get processed

Monks Please help...

Your help in understanding the logic will be greatly appreciated

1. Current_results (File1.txt): Distance Fuel_Consumption 60 2.0 80 2.2 100 3.0 200 5.5 <EOF> 2. Baseline1 (File2.txt): Distance Fuel_Consumption 60 1.0 80 1.5 100 2.3 200 4.2 <EOF> 3. Output (Report.txt) Distance Fuel_Consumption_Curr Fuel_Consumption_Baseline1 60 2.0 1.0 80 2.2 1.5 100 3.0 2.3 200 5.5 4.2 <EOF>

Code written to process the files

#!/usr/bin/perl -w use strict; use Data::Dumper qw(Dumper); my @list_of_files = qw(file1.txt file2.txt); my %data; my $file_num = 0; my $Distance; foreach my $file_name (@list_of_files) { $file_num++; my $file = 'File'.$file_num; my $flag = 0; open FH, $file_name or die "Cannot open $file_name. $!"; while(<FH>){ chomp; if($_ =~ /\s*(\d+)\s*(\d+\.\d+)/){ $Distance=$1; $data{$Distance}{$file}{'Fuel_Consumption'}=$2; next; } } close FH; } print Dumper(\%data); my ($tmpDist,$tmpFile,$tempValue); print "Merged output\n"; foreach $tmpDist (sort{ $a <=> $b } keys( %data ) ) { print "$tmpDist"; foreach $tmpFile (sort{ lc($a) cmp lc($b) } keys( %{$data{$tmpDist +}} ) ) { $tempValue=$data{$tmpDist}{$tmpFile}{'Fuel_Consumption'}; print "\t $tempValue"; } print "\n"; }

Output generated is as follows but without header information is as follows

Header missing is : Distance Fuel_Consumption_Curr Fuel_Consumption_Baseline1

60 2.0 1.0 80 2.2 1.5 100 3.0 2.3 200 5.5 4.2
  • Comment on How to merge the contents of Multiple files stored in the Hash and generate the merged file output along with Header in tabular form
  • Select or Download Code

Replies are listed 'Best First'.
Re: How to merge the contents of Multiple files stored in the Hash and generate the merged file output along with Header in tabular form
by toolic (Bishop) on Mar 08, 2010 at 16:31 UTC
    When you read your input files, you intentionally disregard the header lines because they do not match your regex (/\s*(\d+)\s*(\d+\.\d+)/). Because of that, I presume you know ahead of time what the headers should be. Why not just print them out before your print loop?
    print "Merged output\n"; print "Distance Fuel_Consumption_Curr Fuel_Consumption_Baseline1\n";
    On a side note, Text::Table is handy for printing simple text tables.
Re: How to merge the contents of Multiple files stored in the Hash and generate the merged file output along with Header in tabular form
by NetWallah (Canon) on Mar 08, 2010 at 19:03 UTC
    Not pretty output, but I believe this code meets your spec, and is extensible (Allows unlimited number of files).
    #!/usr/bin/perl use strict; use warnings; my %Consumption; my @headers; foreach my $filename (@ARGV){ Extract_info_From_File_Into_Consumption ($filename); } # Print output print "Dist\t@headers\n"; for my $dist (sort {$a <=> $b} keys %Consumption){ my $line = "$dist\t"; $line .= "\t$_" for @{$Consumption{$dist}}; print "$line\n"; } #---------------- sub Extract_info_From_File_Into_Consumption { my $filename = shift; open my $f , "<", $filename or die "Could not open $filename : $!"; my ($head1,$head2) = split /\s+/ ,<$f>,2 or die "No data in $filena +me"; chomp $head2; my $index = scalar @headers; $headers[$index] = "$head2\_$filename"; while (<$f>){ my ($dist, $consume) = m/(\d+\.?\d*)/g; $Consumption{$dist}[$index] = $consume; } close $f; }
    Update: Simplified code a little (Perhaps more readable), using HOA instead of HOH.
    Update2: added close($f); just to be PC.

         Theory is when you know something, but it doesn't work.
        Practice is when something works, but you don't know why it works.
        Programmers combine Theory and Practice: Nothing works and they don't know why.         -Anonymous

      Thank you very much for the solutions