noob_mas has asked for the wisdom of the Perl Monks concerning the following question:
Hello everyone, i write an executable script where it will take in 3 files as input, 1st file is the user setup file. 2nd and 3rd file is a reference file where, i can get the rest of the data input by the user, at the 1st file. The 1st file will have 2columns <column_a> <column_b> , where whenever if both of this element exist in the file2, it will take a set of data from that line and will store it in an array, then using that array, it will search in file 3 for matching data then it will store it in another array. I do realize that this code can be further optimize, just hope that you guys can give a good point on where to optimise it. Thank you in advance. Snippet of my code is as follow;
#!/tools/perl/5.8.8/linux/bin/perl use strict; #use warnings; use Getopt::Long; my ($file1,$file2,$file3); my $output = 'OUTPUT_FILE';##output file name my $count = 0; GetOptions ('file1=s' => \$file1, 'file2=s' => \$file2, 'file3=s' => \ +$file3); open my $fh1, '<', $file1 or die "Can't open $file1: $!"; #file1 list +by user output list open my $fh2, '<', $file2 or die "Can't open $file2: $!"; #XX.file2 fi +le open my $fh3, '<', $file3 or die "Can't open $file3: $!"; #xx.file3 fi +le open my $fh4, '>', $output or die "Can't open $output: $!"; #output fi +le my ($el1,$el2,$output,$input); my (@data,@file2_data,@array_2,@data_1,@array_4,@data_4,@array_5,@arra +y_6,@data_7,@array_8); my ($input_1,$input_2,$input_3,$input_4,$stage_1,$stage_2,$stage_3,$st +age_4,$output_3,$final_join); my ($marc_data,$marc_add,$V_output,$join_1,$join_2,$join_3,$join_4); my ($data,$address,$stage_output,$element2,$marc_data_1,$marc_add_1,$j +oin_for_2nd_stage); my ($element_4,$file3_data,$file3_add,$dataout,$marc_array,$file3_data +_1,$file3_add_1,$dataout_1,$marc_array_1,$join_1st_stage_file2_file3, +$nothing,$nothing_1,$nothing_2,$nothing_3,$dataout_final,$marc_array_ +final); my @array_1 = <$fh2>; foreach (@array_1) { chomp $_; } #can make this as an option 1st... while (<$fh1>){ ##my config file chomp $_; # print "$_\n"; if ($_ !~ /^\s*$/) { #print "$_\n"; @data = split ' ', $_; $el1 = $data[1]; #output $el2 = $data[0]; #input $output = quotemeta ($el1); $input = quotemeta ($el2); foreach my $element(@array_1) { chomp $element; if ($element !~ /^\s*$/) { if (($element =~ m/$output/) && ($element =~ m/$input/)) { # +print out all the matching output lines @file2_data = split ' ',$element; $marc_data = $file2_data[2]; $marc_add = $file2_data[3]; $V_output = $file2_data[7]; $input_1 = $file2_data[11]; $stage_1 = $file2_data[13]; $input_2 = $file2_data[15]; $stage_2 = $file2_data[17]; $input_3 = $file2_data[19]; $stage_3 = $file2_data[21]; $input_4 = $file2_data[23]; $stage_4 = $file2_data[25]; if ($input_1 =~ m/$input/) { $join_1 = join (" ", ("$input_1","$V_output","$marc_data +","$marc_add","$stage_1")); push (@array_2,$join_1); } elsif ($input_2 =~ m/$input/) { $join_2 = join (" ", ("$input_2","$V_output","$marc_data +","$marc_add","$stage_2")); push (@array_2,$join_2); } elsif ($input_3 =~ m/$input/) { $join_3 = join (" ", ("$input_3","$V_output","$marc_data +","$marc_add","$stage_3")); push (@array_2,$join_3); } elsif ($input_4 =~ m/$input/) { $join_4 = join (" ", ("$input_4","$V_output","$marc_data +","$marc_add","$stage_4")); push (@array_2,$join_4); } } } } } else { } } my @array_4 = <$fh3>; foreach (@array_4) { chomp $_; } foreach (@array_2) { chomp $_; my ($input_2,$output_2,$data,$address,$stage_output_1) = split (/ /,$_ +); foreach my $element_4(@array_4) { chomp $element_4; @data_4 = split ' ', $element_4; $file3_data = $data_4[3]; $file3_add = $data_4[4]; $dataout = $data_4[7]; $marc_array = $data_4[9]; if (($file3_data == $data) && ($file3_add == $address)) { $join_1st_stage_file2_file3 = join (" ", ("$input_2","$output_2","$da +ta","$address","$stage_output_1","$file3_data","$file3_add","$dataout +","$marc_array")); push (@array_5,$join_1st_stage_file2_file3); } } } open my $fh5, '<', $file2 or die "Can't open $file2: $!"; #XX.file2 fi +le my @array_3 = <$fh5>; foreach (@array_3) { chomp $_; } foreach (@array_5) { chomp $_; my($input_2,$output_2,$data,$address,$stage_output_1,$file3_data,$fil +e3_add,$dataout,$marc_array) = split (/ /,$_); $stage_output = quotemeta($stage_output_1); $output = quotemeta($output_2); $input = quotemeta($input_2); foreach my $element_2(@array_3) { @data_1 = split ' ',$element_2; $marc_data_1 = $data_1[2]; $marc_add_1 = $data_1[3]; $output_3 = $data_1[7]; $input_1 = $data_1[11]; $stage_1 = $data_1[13]; if ($output_3 =~ m/$output/) { if (($input_1 =~ m/$input/) && ($stage_1 =~ m/$output/)) { $nothing = 'N/A'; $nothing_1 = 'N/A'; $join_for_2nd_stage = join(" ",("$input_2","$output_2","$data","$a +ddress","$stage_output_1","$file3_data","$file3_add","$dataout","$mar +c_array","$nothing","$nothing_1")); push (@array_6,$join_for_2nd_stage); } elsif (($input_1 =~ m/$stage_output/) && ($stage_1 =~ m/$output/)) + { $nothing = $marc_data_1; $nothing_1 = $marc_add_1; $join_for_2nd_stage = join(" ",("$input_2","$output_2","$data","$a +ddress","$stage_output_1","$file3_data","$file3_add","$dataout","$mar +c_array","$nothing","$nothing_1")); push (@array_6,$join_for_2nd_stage); } } } } open my $fh6, '<', $file3 or die "Can't open $file2: $!"; #XX.file2 fi +le my @array_7 = <$fh6>; foreach (@array_7) { chomp $_; #print "$_\n"; } foreach my $element_6(@array_6) { chomp $_; my($input_2,$output_2,$data,$address,$stage_output_1,$file3_data,$file +3_add,$dataout,$marc_array,$nothing,$nothing_1) = split (/ /,$element +_6); foreach my $element_7(@array_7) { #print "$element_7\n"; @data_7 = split ' ', $element_7; $file3_data_1 = $data_7[3]; $file3_add_1 = $data_7[4]; $dataout_1 = $data_7[7]; $marc_array_1 = $data_7[9]; $nothing_2 = 'N/A'; $nothing_3 = 'N/A'; if(($nothing_2 =~ m/$nothing/) && ($nothing_3 =~ m/$nothing_1/)) { + $dataout_final = 'N/A'; $marc_array_final = 'N/A'; $final_join = join (" ",("$input_2","$output_2","$data","$address" +,"$stage_output_1","$file3_data","$file3_add","$dataout","$marc_array +","$nothing","$nothing_1","$dataout_final","$marc_array_final")); #print "$final_join\n"; push (@array_8,$final_join); last; } elsif(($file3_data_1 =~ m/$nothing/) && ($file3_add_1 =~ m/$nothin +g_1/)){ $dataout_final = $dataout_1; $marc_array_final = $marc_array_1; $final_join = join (" ",("$input_2","$output_2","$data","$address" +,"$stage_output_1","$file3_data","$file3_add","$dataout","$marc_array +","$nothing","$nothing_1","$dataout_final","$marc_array_final")); #print "$final_join\n"; push (@array_8,$final_join); last; } } } print $fh4 " STAGE_N + STAGE_N+1\n"; print $fh4 "<<<<<<INPUT>>>>>>>>. <<<<<OUTPUt>>>>>>> <<<<MARC>>><<<<<<D +ATAOUT>>>>>>>>>>>>>>><<<<<<<<<<<<<<MARC_ARRAY>>>>>>>>>>>><<<<<MARC>>> +>><<<<<DATAOUT>>>>>>>>>>>>>>>><<<<<<<<MARC_ARRAY>>>>>>>>>>>>\n"; print $fh4 " DATA ADD + DATA ADD + \n"; foreach my $element_8(@array_8) { chomp $element_8; #print "$element_8\n"; my($input_2,$output_2,$data,$address,$stage_output_1,$file3_data,$file +3_add,$dataout,$marc_array,$nothing,$nothing_1,$dataout_final,$marc_a +rray_final) = split ' ',$element_8; printf $fh4 "%-25s %12s %4s %6s %-30s %30s %2s %4s %20s %20s\n",$input +_2,$output_2,$data,$address,$dataout,$marc_array,$nothing,$nothing_1, +$dataout_final,$marc_array_final; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Searching for two elements in two different lines
by hdb (Monsignor) on Aug 30, 2013 at 08:32 UTC | |
|
Re: Searching for two elements in two different lines
by Eily (Monsignor) on Aug 30, 2013 at 12:45 UTC | |
|
Re: Searching for two elements in two different lines
by mtmcc (Hermit) on Aug 30, 2013 at 08:45 UTC | |
|
Re: Searching for two elements in two different lines
by jwkrahn (Abbot) on Aug 31, 2013 at 05:01 UTC | |
by noob_mas (Novice) on Sep 03, 2013 at 03:13 UTC |