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

Not able to construct the header information of multiple files from the table derived from the Hash datastructure (hash is built after parsing the extracted data from text file)

Dear monks: Currently I am not able to put the header information while printing the hash table contents based on the parsed data. For example Headers of this kind : Model TYRE_B1 TYRE_B2 TYRE_B2 TYRE_B3 Task i am doing currently is a. parsing the config file and construct the ConfigFile look up table. Config file look up table consists of current file path to compare against any number of old files path having similar content. b. Build the look up table based from the current file data and files provide to compare. c. Once the look table is build, i wanted to print the table in the below format.

Expected table format:

Model TYRE_B1 TYRE_B2 TYRE_B2 + TYRE_B3 100 10 15 16 19 200 20 25 17 30 300 25 26 18 38 Model ENGINE_B1 ENGINE_B2 ENGINE_B2 ENGINE +_B3 100 101 102 103 105 200 301 302 303 305 300 401 402 403 405 Model CHASSIS_B1 CHASSIS_B2 CHASSIS_B2 CHA +SSIS_B3 100 401 402 403 405 200 501 502 503 505 300 601 602 603 605

Currently after coding i am able to print. The code i wrote to print below is provided.

Please help me in constructing the hash table to print the above format with headers in perl Your advice will be greatly appreciated.

100 10 15 16 19 200 20 25 17 30 300 25 26 18 38 100 101 102 103 105 200 301 302 303 305 300 401 402 403 405 100 401 402 403 405 200 501 502 503 505 300 601 602 603 605

Input files : Config file used

File name: config_car.txt CUR_RESULTS=C:\car_data.txt BASE=C:\car_data1.txt BASE=C:\car_data2.txt BASE=C:\car_data3.txt
Current file used to compare against different provided files: car_data.txt CAR_model_100 TYRE : 10 ENGINE : 101 CHASSIS : 401 CAR_model_200 TYRE : 20 ENGINE : 301 CHASSIS : 501 CAR_model_300 TYRE : 25 ENGINE : 401 CHASSIS : 601

BASELINE FILE 1:

CAR_model_100 TYRE : 15 ENGINE : 102 CHASSIS : 402 CAR_model_200 TYRE : 25 ENGINE : 302 CHASSIS : 502 CAR_model_300 TYRE : 26 ENGINE : 402 CHASSIS : 602

BASELINE FILE 2:

CAR_model_100 TYRE : 16 ENGINE : 103 CHASSIS : 403 CAR_model_200 TYRE : 17 ENGINE : 303 CHASSIS : 503 CAR_model_300 TYRE : 18 ENGINE : 403 CHASSIS : 603

BASELINE FILE 3:

CAR_model_100 TYRE : 19 ENGINE : 105 CHASSIS : 405 CAR_model_200 TYRE : 30 ENGINE : 305 CHASSIS : 505 CAR_model_300 TYRE : 38 ENGINE : 405 CHASSIS : 605

Perl code written so far

use warnings; use strict; use Data::Dumper; my ($hashCarRef); my $baselineIndex; my $CurProcessedFile; my (%ConfigInfo,%CarDatabaseLookupTable); ####################### # Main program ####################### ### Call the Function to read the config file and read the path of the + file, from where the data has to be read &ParseConfigFile('./config_car.txt',\%ConfigInfo); #### Print the number of baseline files read ######## print "Number of Baselines: $ConfigInfo{'Data'}{'File'}{'Count'}\n"; #### Construct the lookup table from current file and base files which + contain reference data &ConstructLookUpTable(\%CarDatabaseLookupTable); print Dumper($hashCarRef); #### Print the current result in table format w.r.t to base files prov +ided in the config file my $FileCount; my $sortid; my $curModelnumber; my ($CarComponent,$CarComponentValue); foreach $sortid (sort {$a <=> $b } keys %{$CarDatabaseLookupTable{'C +ar'}}) { foreach $curModelnumber (sort {$a <=> $b } keys %{$CarD +atabaseLookupTable{'Car'}{$sortid}}) { print "$curModelnumber \t\t"; my $FileCount=0; while ($FileCount <= $ConfigInfo{'Data'}{'File'}{ +'Count'}) { if($FileCount == 0) { $CurProcessedFile="Current_Results"; } else { $CurProcessedFile="Baseline_"."$FileCount"; } # Format used to construct look up table {'Car'} +{id}{$ModelNumber}{$CurProcessedFile}{'TYRE/CHASSIS/ENGINE'} ($CarComponent,$CarComponentValue) = each (%{$Car +DatabaseLookupTable{'Car'}{$sortid}{$curModelnumber}{$CurProcessedFil +e}}); print "$CarComponentValue\t\t"; $FileCount++; } # While loop print "\n"; }# Innert For loop print "\n\n"; } # Outer For loop exit; ###################################################### ########### Function to Construct the look up table ###################################################### sub ConstructLookUpTable { print "ConstructLookUpTable....."; ($hashCarRef) = @_; my $ModelNumber; my $CountIndex=0; while($CountIndex <= $ConfigInfo{'Data'}{'File'}{'Count'}) { if($CountIndex == 0) { # Process first Current Results $CurProcessedFile="Current_Results"; } else { $CurProcessedFile="Baseline_"."$CountIndex"; } print "$CurProcessedFile : $ConfigInfo{'Data'}{$CurProcessedFile}{'P +ath'}\n"; open FILEDELT, $ConfigInfo{'Data'}{$CurProcessedFile}{'Path'} or die + "can't open the file $!\n"; while(<FILEDELT>) { chomp($_); if($_ =~ /CAR_model_(\d+)/) { $ModelNumber = $1; next; } if($_ =~ /TYRE\s*\:\s*(\d+)/) { $hashCarRef->{'Car'}{1}{$ModelNumber}{$CurProcessedFile}{'TYRE +'}=$1; next; } if($_ =~ /ENGINE\s*\:\s*(\d+)/) { $hashCarRef->{'Car'}{2}{$ModelNumber}{$CurProcessedFile}{'ENG +INE'}=$1; next; } if($_ =~ /CHASSIS\s*\:\s*(\d+)/) { $hashCarRef->{'Car'}{3}{$ModelNumber}{$CurProcessedFile}{'CHA +SSIS'}=$1; next; } } # End of While <FILEDELT> $CountIndex++; } # End of While loop to read each file interating through each file close(FILEDELT); print "Done\n"; } ###################################################### ########### Function to Parse the config file ###################################################### sub ParseConfigFile { my ($ConfigFile,$hashRef)=@_; open(CONFIGFILE,"<$ConfigFile") or die $!; my $baselineIndex=0; while(<CONFIGFILE>) { chomp($_); if(($_ eq '') || ($_ =~ m/^;/)) {next;} if($_ =~ m/CUR_RESULTS=(.*)/) { $hashRef->{'Data'}{'Current_Results'}{'Path'}=$1; # Convert Windows format to Unix Format $hashRef->{'Data'}{'Current_Results'}{'Path'} =~ tr/\\/\// +; $hashRef->{'Data'}{'File'}{'Count'}=0; next; } if($_ =~ m/BASE=(.*)/) { $baselineIndex++; $hashRef->{'Data'}{"Baseline_$baselineIndex"}{'Path'}=$1; # Convert Windows format to Unix Format $hashRef->{'Data'}{"Baseline_$baselineIndex"}{'Path'} =~ t +r/\\/\//; $hashRef->{'Data'}{'File'}{'Count'}=$baselineIndex; next; } } }
  • Comment on Not able to construct the header information of multiple files from the table derived from the Hash datastructure (hash is built after parsing the extracted data from text file)
  • Select or Download Code

Replies are listed 'Best First'.
Re: Not able to construct the header information of multiple files from the table derived from the Hash datastructure (hash is built after parsing the extracted data from text file)
by Svante (Sexton) on Mar 10, 2010 at 17:44 UTC

    That is a lot of code for that task, I believe. I couldn't get through all of that to see where the header line would have to printed, so I just quickly hacked out how I would do this:

    use strict; use warnings; # Read configuration file my @files; my $filecounter = 1; open (my $config_fh, "<", "config_car.txt") or die "Could not open configuration file: $!\n"; while (<$config_fh>) { next unless /=/; (my $line, my $name) = split (/\s*=\s*/); chomp $name; my $abbr; if ($line =~ /CUR_RESULTS/) { $abbr = "_C0"; } else { $abbr = "_B" . $filecounter++; }; push @files, {"line" => $line, "name" => $name, "abbr" => $abbr}; }; close $config_fh; # Create data from files my %models; foreach my $file (@files) { open (my $fh, "<", $file->{"name"}) or die qq(Could not open file $file->{'name'}: $!\n); my $modelname; while (<$fh>) { m(CAR_model_) and do {(undef, $modelname) = split (/_model_/); chomp $modelname; next; }; m(:) and do {(my $feature, my $value) = split (/\s*:\s*/); chomp $value; $models{$modelname}{$file->{"abbr"}}{$feature} = +$value; next; }; } close $fh; } # Output table my @modelnames = sort keys %models; foreach my $feature (qw(TYRE ENGINE CHASSIS)) { print "Model"; foreach my $file (@files) { printf "%12s%s", $feature, $file->{"abbr"}; # Header line } print "\n"; foreach my $modelname (@modelnames) { print $modelname; foreach my $file (@files) { printf "%15s", $models{$modelname}{$file->{"abbr"}}{$featu +re}; } print "\n"; } print "\n"; }

    Note that I eliminated the duplicate headers (you wrote two "B2" headers each time) and put one for the "current case" in instead.