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

In reply to 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 Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.