http://qs321.pair.com?node_id=629097

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

Dear Monks, I'm writing a script to parse a csv file (control file) and build a data structure.

I have created the sample script to create the data structure as given below.
I'd like to simplify the parsing procedure im my sample code(currently the function is 100 lines) and make it more robust.

Below is the structure of the control file
Tags,Marker,Description,Currency,Industry,SuperSector,Sector,Sub Curre +ncies FWA,WORLDS,World Index,USD,0001|1000,,0530|1770, FBRIC,AWBAX,All-World BRIC Index,USD,,,, FWBRA,WIBRE,Brazil Index,USD|9LC
The program should build the structure as below:
{ 'SectorCurrency' => '', 'Currency' => 'USD', 'SourceID' => 'WORLDS', 'Name' => 'World Index', 'ExchangeTag' => 'FWA' }; { 'SectorCurrency' => '', 'Currency' => 'USD', 'SourceID' => 'WORLDS', 'IndustryCode' => '0001', 'Name' => 'FTSE World Index', 'ExchangeTag' => 'FWA0001' }; { 'SectorCurrency' => '', 'Currency' => 'USD', 'SourceID' => 'WORLDS', 'IndustryCode' => '1000', 'Name' => 'FTSE World Index', 'ExchangeTag' => 'FWA1000' }; { 'SectorCurrency' => '', 'SectorCode' => '0530', 'Currency' => 'USD', 'SourceID' => 'WORLDS', 'Name' => 'FTSE World Index', 'ExchangeTag' => 'FWA0530' }; { 'SectorCurrency' => '', 'SectorCode' => '1770', 'Currency' => 'USD', 'SourceID' => 'WORLDS', 'Name' => 'FTSE World Index', 'ExchangeTag' => 'FWA1770' }; { 'SectorCurrency' => '', 'Currency' => 'USD', 'SourceID' => 'AWBRIC', 'Name' => 'FTSE All-World BRIC Index', 'ExchangeTag' => 'FBRIC' }; { 'SectorCurrency' => undef, 'Currency' => 'USD', 'SourceID' => 'WIBRA', 'Name' => 'FTSE Brazil Index', 'ExchangeTag' => 'FWBRA' }; { 'SectorCurrency' => undef, 'Currency' => '9LC', 'SourceID' => 'WIBRA', 'Name' => 'FTSE Brazil Index', 'ExchangeTag' => 'FWBRA_L' };
Parsing Rules:
1) Script should create ExchangeTag based on the "Industry" , "Currency", "Sector". ex 1: The first Tag "FWA" in the config file should build ExchangeTags 'FWA', 'FWA0001',
'FWA1000', 'FWA0530', 'FWIA770' (as this "Tag" has Industry and Sectors defined.

ex 2: The third Tag "FWBRA" in config file should build ExchangeTags 'FWBRA' and 'FWBRA_L'
(because there are two currencies USD/9LC). The "L" appended to ExchangeTag is the second character from the Currency "9LC".

Below is the my test code to parse according to the rules
#!/bin/perl use strict; use warnings; my $cfg_file = "/tmp/Allcontrol.csv"; read_control($cfg_file); sub read_control { my ($cfg_file) = @_; ## This is a internal module to parse and giveout data as hashref my $ctrl_data = ImportData(); my @IndxData; foreach (keys %$ctrl_data){ my $ExchangeTag = $ctrl_data->{$_}->{Tag}; my $Marker = $ctrl_data->{$_}->{Marker}; my $Name = $ctrl_data->{$_}->{Name}; my $Currency = $ctrl_data->{$_}->{Currency}; my $Industry = $ctrl_data->{$_}->{Industry}; my $SuperSector = $ctrl_data->{$_}->{SuperSector}; my $Sector = $ctrl_data->{$_}->{Sector}; my $Ind_Sec_Curr= $ctrl_data->{$_}->{Sub Currencies}; # Parse single/multiple currencies if(defined $Currency){ my @Currency = split /\|/, $Currency; foreach my $corecurr (@Currency){ my $row = {}; my $curr = substr($corecurr, 0,1); $curr = substr($corecurr, 1,1) if($curr eq "9"); if($curr eq "U"){ $row->{ExchangeTag} = $ExchangeTag; }else { $row->{ExchangeTag} = $ExchangeTag . "_" . $cu +rr; } $row->{Name} = $Name; $row->{Currency} = $corecurr; $row->{SectorCurrency} = $Ind_Sec_Curr; $row->{SourceID} = $Marker; push @IndxData, $row; } } #Parse single/multiple industries if (defined $Industry and $Industry ne ""){ my @industry = split /\|/, $Industry; foreach my $ind (@industry){ my $row = {}; $row->{Name} = $Name; $row->{Currency} = $Currency; $row->{SectorCurrency} = $Ind_Sec_Curr; $row->{IndustryCode} = $ind; $row->{SourceID} = $Marker; $row->{ExchangeTag} = $ExchangeTag . $ind; push @IndxData, $row; } } #Parse multiple/single Sectors if (defined $Sector and $Sector ne ""){ my @sector = split /\|/, $Sector; foreach my $sect (@sector){ my $row = {}; $row->{Name} = $Name; $row->{Currency} = $Currency; $row->{SectorCurrency} = $Ind_Sec_Curr; $row->{SectorCode} = $sect; $row->{SourceID} = $Marker; $row->{ExchangeTag} = $ExchangeTag . $sect; push @IndxData, $row; } } } die Dumper @IndxData; }