Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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; }

In reply to Suggestions for simplifying script to parse csv data by chanakya

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-06-21 17:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.