Tara has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I have just registered to the site and this is my first post. I am Perl newbie. Recently wrote a script to create an extract out of the existing extract. The requirement is to pickup few columns as specified by client. It sounds simple but here is the twist... we have to lookup more than one extract to create a final file. For example: account file will contain 100 columns , get few from main account file it. Then for the corresponding account's client, get client details from another file. This way we have to lookup 4 to 5 different files for complete information.
The design we suggested - Have a configuration file for fields information. In that specify for a field - file name, which column to be extracted from this, if this main file or lookup file, if lookup then lookup key.
ACCT NUMBER|positionfile.delim|2|6
ACCT SHORT NAME|accountfile.delim|6|6
PO TYPE|positionfile.delim|2|3
CUSIP|securityfile.delim|8|5
LOC CODE|positionfile.delim|2|47
LOC NAME|locationfile.delim|47|4
How to read it -
at the start it is specified main file is account file. start reading that file line by line
Then
for ACCT NUMBER read main file, second column in main file is primary key for looking up positionfile.delim. Grep that and extract 6th column.
Similarly LOC NAME, get the 47 column from main file, use that value as key in locationfile.delim and extract 4th column. and so on...
The main problem is though I have written a working code for this, the speed is lethargic.
Please suggest how to improve it or any better design. Delimter is pipe which is provided from another config file to support various types of delimited files.
###Reading layout while (<$CONFIG>){ chomp; my @list=split(/$hash_ref->{DELIMITER}/); $file_list = $list[1]; $file_list =~ s/CCYYMMDD/$date/g; push @{$look_info{$list[0]}},$file_list."-".$list[2]." +-".$list[3]; push (@header, $list[0]); } ###reading data file while (<$SRC_FILE>){ @data=split(/$hash_ref->{DELIMITER}/,$_); while ( ( my $column) = each %look_info){ foreach my $pos_info (@{$look_info{$column}}){ @look_here = split(/\-/, $pos_info); #print "look here : @look_here \n"; $lkp_file_name = $look_here[0]; $key_location = $look_here[1]; $data_location = $look_here[2]; $key_string = $data[$key_location-1] ; chomp $key_string; print "key_string : $key_string \n"; my $key_pattern = "|".$key_string."|"; #print "key string is : $key_pattern \ +n"; $final_data = ` grep "$key_pattern" "$ +{indir}/${lkp_file_name}" |cut -d"|" -f"$data_location"`; #print "data value is : $final_data \n +"; chomp $final_data; push @{$final_record{$column}},$final_ +data."|"; } } } ###printing final file my $i =0; my @currdata; $headerstring = join('|', @header); print $OUTPUT_FILE $headerstring."\n"; while(1){ foreach my $head (@header){ chomp $head; $head =~ s/\\//g; @currdata = @{$final_record{$head}}; print $OUTPUT_FILE "$currdata[$i]"; } print $OUTPUT_FILE "\n"; $i++; if ($i>($#currdata)){ last; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Script performance issue
by Athanasius (Archbishop) on Dec 15, 2015 at 12:49 UTC | |
by Tara (Initiate) on Dec 17, 2015 at 12:39 UTC | |
by Athanasius (Archbishop) on Dec 17, 2015 at 13:54 UTC | |
|
Re: Perl Script performance issue
by Cristoforo (Curate) on Dec 15, 2015 at 15:12 UTC | |
by AnomalousMonk (Archbishop) on Dec 15, 2015 at 15:30 UTC | |
by Tara (Initiate) on Dec 16, 2015 at 08:58 UTC | |
|
Re: Perl Script performance issue
by Laurent_R (Canon) on Dec 15, 2015 at 13:49 UTC | |
by Tara (Initiate) on Dec 16, 2015 at 07:39 UTC | |
by poj (Abbot) on Dec 16, 2015 at 08:46 UTC | |
by Tara (Initiate) on Dec 16, 2015 at 08:56 UTC | |
by poj (Abbot) on Dec 16, 2015 at 09:05 UTC | |
by Laurent_R (Canon) on Dec 16, 2015 at 18:30 UTC | |
|
Re: Perl Script performance issue
by GrandFather (Saint) on Dec 15, 2015 at 20:11 UTC | |
by dasgar (Priest) on Dec 16, 2015 at 05:36 UTC | |
by Laurent_R (Canon) on Dec 16, 2015 at 07:34 UTC | |
|
Re: Perl Script performance issue
by poj (Abbot) on Dec 15, 2015 at 12:51 UTC |